We are threading legacy code and have encountered the following:
We want to thread this loop. Not surprisingly, the SAVEd variables get scrambled between (?) threads and the results are rubbish. We have changed the code to write the data previously SAVEd to a temp file (unique to each thread) and (re)read it as necessary. Naturally this is very much slower and defeats any improvement offered by threading. The routine in questions has been declared RECURSIVE. We are using OpemMP.
Are there alternatives to writing/reading temp files?
Create an array of the SAVEd items, indexed by the thread_id, ie a unique identifier of each thread, then reference the quantities by active thread, ie myvar(this_thread).
I have found it useful to create a UDT array, call it thread_args(), which includes all the items which are thread-specific to the calculational activity. If you make this array a target, then your local variables can point to the safe quantities, ie, myvar => thread_args(this_thread)%myvar, and you can put this in once at the top, no change to the rest of the code.
You can declare SAVEd variables as threadprivate. Each thread would have its own copy of the variable. For example:
integer,save:: ii real,save:: aa !$OMP threadprivate(ii,aa)