0

My program is like this:

int *array;
#pragma omp threadprivate(array1)
int main()
{
    array = new int[N];//N is already known.
    func_A();
    ...
}
void func_B()
{ 
    func_C();
    ......
}
void func_C()
{
    do something to array(write to array);
}
void func_A()
{
   #pragma omp parallel copyin(array)
   #pragma omp parallel for 
   for(i = 0 ; i < n ; i ++)
      func_B();
}

when I debug(VS 2012) the program and watch(is it clear?) it,compiler always says there is no such variable.Who can tell me where the wrong is and how to solve it ?

I have a big graph demonstrated by an Adjacent Matrix.I want to find out how many sub-graphs with specific shape in the graph.Because it takes a long time , so I want to use OpenMP ,and I want to give each thread a copy of this graph. I did this as my codes show,but a lot of memory problems were made.This is what I want to do.

2
  • You need to update your question to state what you're trying to do NOT how you are trying to do it. You question currently is a classic xy problem. Delete your added content and explain what you're trying to do. Commented Jul 25, 2014 at 6:53
  • I added my purpose at the end of my question. Commented Jul 25, 2014 at 7:07

1 Answer 1

1

Your code is going to make private copies of the pointer to the array not the array. The only reason this would be useful is if you wanted to have different pointers per thread to the same array across parallel regions. I suspect that you're really trying to do something else. If you really want to have different private copies of the dynamic array across parallel regions then you need explicitly allocate them in the parallel regions and copy the values as follows.

#include <stdio.h>
#include <omp.h>
#include <string.h>

int *array, *array_private;
#pragma omp threadprivate(array_private)

#define N 100

int main() {
    array = new int[N];

    #pragma omp parallel
    {
        array_private = new int[N];
        memcpy(array_private, array, sizeof(int)*N); //or use std::copy     
    }

    #pragma omp parallel
    {
        delete[] array_private;
    }
    delete[] array;
}

Note that OpenMP in C/C++ treats dynamic arrays and static arrays differently. Dynamic arrays are treated as pointers and static arrays are treated like an array. So copyin on a static array actually copies all the elements of the array instead of the pointer value.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi,Z boson,I did as you say(with a little difference) and meet another problem.
Can you please describe what you're trying to do in your question? Also, what do you mean ("with a little difference") and "meet another problem".
Sorry, I'm not very skilled in this 'comment' area :(.I did like this: I allocate them in a parallel region and find that every thread had its own copy of array.(Evidence is that "thread_num = 1,array = 00363420;thread_num = 2,array = 0037EB68",they are different ,so I can make sure that each thread has its own copy of array.") But when I debug this , I found in func_A, array of thread A and array of thread B are the same! I don't know why ,the code is like the one in my question above. 'with a little difference' is I directively made array threadprivate not using the sentence'memcpy()...'
@BecomeBetter, click on edit on your question and provide more information to your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.