openmp实验

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
44
45
46
#include "stdio.h"
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<time.h>
#include<stdlib.h>
#include <unistd.h>
int main()
{
int nthreads, tid;
int nprocs;
char buf[32];
omp_set_num_threads(11);
int q[20];
for(int i=0;i<20;i++)
q[i]=rand()%1000000;
int wait=1;
int current=0;
#pragma omp parallel private(nthreads, tid)
{
tid = omp_get_thread_num();
if (tid==0) {
int c=0;
while(c<19){
printf("question %d : %d + %d = ?n",c/2+1,q[c],q[c+1]);
c+=2;
}
wait=0;
}else{
#pragma omp critical(a)
{
while(true){
if(wait==0){
printf("my student number is %d question%d's answer is %d n",tid,current/2+1,q[current]+q[current+1]);
current+=2;
break;
}
}
}
}
}
return 0;
}