Reputation: 11
I'm using OpenMP in c++. I want to specify the number of threads (16) and then run each thread with an id from 0 to 15. I have an array of 16 numbers which I want to use as parameters in each separate thread. So I have
omp_set_dynamic(0);
omp_set_num_threads(16);
int tid;
#pragma omp parallel shared(myvector) private(tid)
{
tid = omp_get_thread_num();
// Do some calculations using myvector[tid]
}
However, the (tid)s are not ordered as 0 to 15, as some are repeated twice or more. How do I force it to run one task with each of the 16 parameters?
Upvotes: 1
Views: 303
Reputation: 242
Each thread in the team executes all statements within a parallel region except for work-sharing constructs.
I tried this simple code
#include <omp.h>
#include <stdio.h>
int main(){
omp_set_dynamic(0);
omp_set_num_threads(32);
int tid;
double *myvector;
#pragma omp parallel shared(myvector) private(tid)
{
tid = omp_get_thread_num();
printf("Thread number = %d\n",tid);
// Do some calculations using myvector[tid]
}
return 0;
}
and got right result:
Thread number = 3
Thread number = 4
Thread number = 0
Thread number = 5
Thread number = 6
Thread number = 7
Thread number = 8
Thread number = 9
Thread number = 10
Thread number = 11
Thread number = 12
Thread number = 13
Thread number = 14
Thread number = 15
Thread number = 1
Thread number = 2
Please, check it on your machine or write more information about code you tried and output you got.
Upvotes: 1