1

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?

3
  • How long do your calculations take? How many threads does your CPU support? It's possible that, for example, each OMP thread runs two of your calculations, do the tids would be duplicated. Have you tried a (for) loop? Commented Apr 14, 2017 at 0:36
  • I'm running on a cluster so it has 16 processors per node. Is there a way to force them to run one calculation per thread even if they don't take long? (They take anywhere from a minute to hours, depending on the dataset) Commented Apr 14, 2017 at 0:40
  • I'm afraid the question is unclear, not reproducible and extremely broad. You have to be more precise with describing what you do, what you expect and what you observe. Provide a minimal reproducible example with an exact description how you compile and execute it and what environment you run it on (compiler!). Are you running on multiple nodes? What is a "calculation"? Commented Apr 14, 2017 at 11:52

1 Answer 1

1

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.

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

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.