-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_check_err_hello.c
43 lines (34 loc) · 1.07 KB
/
my_check_err_hello.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
// 检查是否定义
#ifdef _OPENMP
# include <omp.h>
#endif
void Usage(char* prog_name);
void Hello(int thread_count); /* Thread function */
int main(int argc, char* argv[]) {
int thread_count;
if (argc != 2) Usage(argv[0]);
thread_count = strtol(argv[1], NULL, 10);
if (thread_count <= 0) Usage(argv[0]);
# pragma omp parallel num_threads(thread_count)
Hello(thread_count);
return 0;
} /* main */
void Usage(char *prog_name) {
fprintf(stderr, "usage: %s <thread_count>\n", prog_name);
fprintf(stderr, " thread_count should be positive\n");
exit(0);
} /* Usage */
void Hello(int thread_count) {
# ifdef _OPENMP
int my_rank = omp_get_thread_num();
int actual_thread_count = omp_get_num_threads();
# else
int my_rank = 0;
int actual_thread_count = 1;
# endif
if (my_rank == 0 && thread_count != actual_thread_count)
fprintf(stderr, "Number of threads started != %d\n", thread_count);
printf("Hello from thread %d of %d\n", my_rank, actual_thread_count);
} /* Hello */