OpenMP - Initializing 1D Array

Viewed 1184

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 Answers
Related