warning: 'k' may be used uninitialized in this function (?!?)

Hi everyone,

I have a function that creates an integer called "k" and uses it to count through some parallelised loops (using openMP). However when I try to compile it I get a warning saying "warning: 'k' may be used uninitialized in this function" and an error saying "note: 'k' was declared here".

The first statement is false but probably to do with the fact that the compiler might not understand what openmp is doing, but why the compiler is stopping me from compiling just to tell me that I'm declaring a variable is beyond me. Here is a chopped down version of the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
bool train() {

    int j_out, k, iter;

    // Run initial training cycle for all brains
    cout << "Running initialisation cycle...\n";
    #pragma omp parallel private(k)
    {
        #pragma omp for schedule(static) nowait
        for (k = 0; k < B; k++) {
            (*community[k]).fitness = 1 / run_cycle(community[k], TRAIN_STEP_START, TRAIN_STEP_END);
        }
    }

    while (check_file("stop") == 0) {
        //parallelised mutation
        #pragma omp parallel private(iter)
	{
	    #pragma omp for schedule(static) nowait
	    for (iter = 0; iter < MUTES; iter++) {
	        if (brain_order[iter] == k_train_top) k_train_top = -1;
                mutate( community[brain_order[B - 1 - iter]], community[brain_order[iter]] );
	    }
	}

        //some more code here (nothing to do with k)...

        // parallelised training
        #pragma omp parallel private(iter,k)
        {
            #pragma omp for schedule(static) nowait
            for (iter = 0; iter < MUTES; iter++)
                k = brain_order[iter];
                (*community[k]).fitness = 1 / run_cycle(community[k], TRAIN_STEP_START, TRAIN_STEP_END);
        }
    }

    return 0;
}


compiler errors are usually helpful and 99% of the time I find the errors myself, but I just don't understand what's happening here (i've loaded the logomp libraries and I don't get any openmp-related errors). I'm using windows 7 64-bit with the GNU GCC compiler (on code blocks). Thanks in advance for the help!
I've not used threads much at all, but you may want to declare k within its threads scope. Sharing a resource available to all threads at any point in time without locking that resource would make things nasty, it might be what that message is being triggered for.

for (int k = 0; k < B; k++)

Remove previous deceleration.
Bah, the problem was the lack of curly brackets after the declaration of the last for loop. it was breaking the paralellisation by looking only the declaration of k through each instance of the loop, not the call to run_cycle. My bad. thanks for the help though :)
Intriguing, does the implementation make a copy of all the variables from the wider scope and throw them into their own thread?
Topic archived. No new replies allowed.