Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

Saving and Restoring Linked list-structure from file

jansson
Beginner
479 Views
Hi,
I want to save the data in my program to file and later read it back after restarting the program in a hopfully portable way.
My data is in a tree (+ linked list) like structure with allocted pointers and home made TYPEs.
Does anyone have a idea about where to start with this feature using Fortran 90/95 ?
Cheers/Magnus


0 Kudos
3 Replies
Steven_L_Intel1
Employee
479 Views
You can't do that. Pointers cannot be reliably (or legally) written to and read from files. Two approaches come to mind. One is to create a big chunk of memory with the values laid out in it and use offsets within the chunk rather than absolute addresses, the other would be to have a nested text representation such as one based on XML. Both will require you to write code that converts the structures for input and output.
0 Kudos
jimdempseyatthecove
Honored Contributor III
479 Views

I imagine your application will periodically checkpoint the current state and then continue to run. Should you experience some sort of failure, or required suspension such as nightly backup, you would restore to most recent (good) check point and resume. The Fortran application I use for Space Elevator simulation does just this. (simulation runs can go for days, weeks on quad core systems, and I do have occasional power outages).

In my case I am not so much tree structure but there are pointers to other structures or arrays of structures. In your case you might consider (you may have done most of this already) creating a module for managing the tree and nodes contained within the tree. (as opposed to dispersing this code throught the application). This module would contain the standard routines: allocate node, deallocate node, insert node, find node, then you would add backup nodes, and restore nodes.

If your insert node is relatively fast (or fast enough for your intermittant restores) then the backup nodes could traverse the tree writing every node, including a (or multiple)usless like pointers. The restore nodes would read the list of nodes and then perform an insert (recreating new linkages).

In the code I wrote for doing this, the backup data set would contain diagnostic information along with the backup data. I would emit 8 bytes of text pluse a numeric value followed by zero or more additional 8 bytes of text pluse a numeric value followed by the raw data. Example: "Tethers", 8, "Tether", 1, {tether data}, "Tether", 2, {tether data}, ..., "Tether", 8, {tether data}, "Objects", 12, "Object", 1, {object data}

and where {tether data} contains similar structure ("tag", number/count, {stuff}), where {stuff} is raw data or {"tag",number/count, {more stuff}).

Although inserting the "tag",number/count into the dump stream consumes space and adds overhead to the dump and restore time it has two redeeming values:

a) as a debugging aid/sanity check that the data you are reading belongs to the data set of which you believe it to be.

b) possibly more importantly, should your program crash due to a bug or some sort of configuration error, or some sort of goof-up by the operator/system admin, you may be able to fix the application and then resume using the snapshot data set made from the earlier revision of the application.

Jim Dempsey
0 Kudos
jansson
Beginner
479 Views
Thank you Steve and Jim!
I will go back to my drawing board and consider whether to store the objects in my tree in a large "integer" array which then goes to file or object by object in a XML like way with some "tags" for safety. Im sure I will implement some bugs too, so safeguards is a good idea.

Regards,
Magnus Jansson
0 Kudos
Reply