2

I am trying to initialize a dynamic array using OpenMP in C but it seems to be slower than the serial method. The function I am using is

int* createArray(int size, int num) {
    int i;
    int* M = (int*)malloc(size*sizeof(int));

    srand(time(NULL));
    double start = omp_get_wtime();

    #pragma omp parallel for num_threads(num)
    for (i = 0; i < size; i++) {
        M[i] = rand() % (MAX_NUMBER - MIN_NUMBER + 1) + MIN_NUMBER;
    }

    double end = omp_get_wtime();
    printf("Create Array %f\n",end-start);
    return M;
}

I get an array of the given size containing random numbers but the fewer threads I use the faster the function is. Am I doing something wrong?

2

2 Answers 2

1

In general, a parallel application running slower than the corresponding sequential implementation usually comes from either the overhead of starting the threads or the bottleneck of having threads that are not perfectly independent (e.g. through shared data).

Here, the former is true because you are calling rand(). This function uses somehow global variables which have to be shared between threads. A way to overcome this would be to use a private seed for each thread. Furthermore, did you notice that your array is not really random when you have multiple threads? You could make the seed provided to srand() a function of omp_get_thread_num() to solve this.

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

Comments

0

I'm quite sure your program is suffering a problem called "false sharing" cache.

The article below explains it quite well. https://software.intel.com/en-us/articles/avoiding-and-identifying-false-sharing-among-threads

this often affects performance a lot. you can quickly have a test. add below to your omp pragma schedule(static, 16)

this should improve a lot. then you can dig further about false sharing.

Comments

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.