-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathomp_hello.cpp
More file actions
42 lines (35 loc) · 924 Bytes
/
omp_hello.cpp
File metadata and controls
42 lines (35 loc) · 924 Bytes
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
#include <iostream>
#include <cstdlib>
#ifdef _OPENMP
#include <omp.h>
#endif
void Hello(void); /* thread function */
int main(int argc, char* argv[])
{
if (argc != 2)
{
std::cout << "Enter the number of threads you want to create!" << std::endl;
exit(EXIT_FAILURE);
}
/* Get the number of threads from command line */
int thread_count = strtol(argv[1], NULL, 10);
if (thread_count <= 0)
{
std::cout << "The number of threads should be positive!" << std::endl;
exit(EXIT_FAILURE);
}
# pragma omp parallel num_threads(thread_count)
Hello();
return 0;
} /* main */
void Hello(void)
{
#ifdef _OPENMP
int my_rank = omp_get_thread_num();
int thread_count = omp_get_num_threads();
#else
int my_rank = 0;
int thread_count = 1;
#endif
std::cout << "Hello from " << my_rank << " of " << thread_count << std::endl;
} /* Hello */