Allow a constant string to be globally accessed by various threads calling a function.
I have the following code which chooses a random char from a list of chars (the permitted alphabet) and appends it to form a random word. I am using this for a genetic algorithm but when I am trying to parallelize it to make it faster (homework assignment) but it ends up going slower. After asking my professor and fixing most of the problems, the last detail is that my alphabet doesn't allow multiple threads to read at the same time.
I have tried defining GENES then using #pragma omp threadprivate(GENES)
which is the command that my professor said would work.
I tried
const string GENES = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRSTUVWXYZ"
#pragma omp threadprivate(GENES)
Which returns this when compiled.
error: ‘GENES’ declared ‘threadprivate’ after first use
I also tried
string GENES;
#pragma omp threadprivate(GENES)
GENES = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRSTUVWXYZ;
which returns:
error: ‘GENES’ declared ‘threadprivate’ after first use
#pragma omp threadprivate(GENES)
^
error: ‘GENES’ does not name a type
GENES = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRSTUVWXYZ 1234567890, .-;:_!\"#%&/()=?@${[]}";
Finally I tried defining GENES in the threads and passing them as a parameter or hard encoding them into the function. All of these make the program slower.
I expected the program to go faster, but it actually goes slower, from 0.5 sec to 1-3 sec after adding threads. I checked and this is not due to the time in creating the threads.
This should be fixed by making the string accessible by all threads but I can't seem to compile and run any solutions I have found successfully.