mirror of
https://github.com/OpenMP/Examples.git
synced 2025-04-07 23:22:11 +01:00
42 lines
800 B
C
42 lines
800 B
C
/*
|
|
* @@name: parallel.1c
|
|
* @@type: C
|
|
* @@compilable: yes
|
|
* @@linkable: yes
|
|
* @@expect: success
|
|
*/
|
|
#include <omp.h>
|
|
|
|
void subdomain(float *x, int istart, int ipoints)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < ipoints; i++)
|
|
x[istart+i] = 123.456;
|
|
}
|
|
|
|
void sub(float *x, int npoints)
|
|
{
|
|
int iam, nt, ipoints, istart;
|
|
|
|
#pragma omp parallel default(shared) private(iam,nt,ipoints,istart)
|
|
{
|
|
iam = omp_get_thread_num();
|
|
nt = omp_get_num_threads();
|
|
ipoints = npoints / nt; /* size of partition */
|
|
istart = iam * ipoints; /* starting array index */
|
|
if (iam == nt-1) /* last thread may do more */
|
|
ipoints = npoints - istart;
|
|
subdomain(x, istart, ipoints);
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
float array[10000];
|
|
|
|
sub(array, 10000);
|
|
|
|
return 0;
|
|
}
|