#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
void Hello(int thread_c);
int main(int argc, char* argv[]) {
int thread_count = strtol(argv[1], NULL, 10);
// printf("thread_count is %d \n", thread_count);
Hello(thread_count);
return 0;
}
void Hello(int thread_c) {
omp_set_num_threads(thread_c);
#pragma omp parallel
{
int my_rank = omp_get_thread_num();
int thread_count = omp_get_num_threads();
printf("Hello from thread %d of %d \n", my_rank, thread_count);
}
}
终端执行:
gcc -g -Wall -fopenmp -o omp_hello omp_hello.c
./omp_hello 4
输出为:
Hello from thread 3 of 4
Hello from thread 1 of 4
Hello from thread 2 of 4
Hello from thread 0 of 4