CS计算机代考程序代写 compiler b’omp_examples.tar.gz’

b’omp_examples.tar.gz’

#include
#include
#include

int main(int argc, char **argv) {

int x = 0;

#pragma omp parallel num_threads(6)
{

//#pragma omp critical
{
x = x + 1;
}

}

printf(“Value of x is %d\n”, x);

return 0;
}

#include
#include
#ifdef _OPENMP
#include
#endif

int fib(int n) {

int i, j;

if (n < 2) return n; else { #pragma omp task shared(i) i = fib(n-1); #pragma omp task shared(j) j = fib(n-2); #pragma omp taskwait return i+j; } } int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: %s \n”, argv[0]);
}

int n = atoi(argv[1]);
int fibterm = fib(n);
fprintf(stderr, “%dth term in Fib series is %d\n”, n, fibterm);
}

#include
#include
#include
#include

int main(int argc, char **argv) {

/* create an array of size 1 million */
int *A;
A = (int *) malloc(1000000 * sizeof(int));
assert(A != NULL);
int j;
for (j=0; j<1000000; j++) { A[j] = (j % 8); } int sum = 0; #pragma omp parallel num_threads(4) { /* loop iteration variable should always be local */ int i; int tid = omp_get_thread_num(); //int sum_local = 0; /* "omp for" is a work-sharing construct */ #pragma omp for for (i=0; i<1000000; i++) { #pragma omp critical sum += A[i]; } printf("tid %d, sum %d\n", tid, sum); } printf("outside the loop, sum %d\n", sum); free(A); return 0; } #include
#include
#include

int main(int argc, char **argv) {

int x = 0;
if (argc == 2)
x = atoi(argv[1]);

#pragma omp parallel num_threads(4)
{
int tid1 = omp_get_thread_num();
int nthreads1 = omp_get_num_threads();

#pragma omp parallel num_threads(2)
{
int tid2 = omp_get_thread_num();
int nthreads2 = omp_get_num_threads();

printf(“Hello world from thread tid1 %d tid2 %d of %d\n”,
tid1, tid2,
nthreads1*nthreads2);

}
}

return 0;
}

// omp_ordered.cpp
// compile with: /openmp
#include
#include

static float a[1000], b[1000], c[1000];

void test(int first, int last)
{

#pragma omp for schedule(static) ordered
for (int i = first; i <= last; ++i) { // Do something here. if (i % 2) { #pragma omp ordered printf("test() iteration %d\n", i); } } } void test2(int iter) { #pragma omp ordered printf("test2() iteration %d\n", iter); } int main( ) { int i; #pragma omp parallel { test(1, 8); #pragma omp for ordered for (i = 0 ; i < 5 ; i++) test2(i); } } #include

void some_other_func(int i) {
printf(“hello from some_other_func %d\n”, i);
}

#include
#include
#include
#include

int main(int argc, char **argv) {

#pragma omp parallel num_threads(4)
{

int tid = omp_get_thread_num();
int nthreads = omp_get_num_threads();

#pragma omp single
{
fprintf(stderr, “thread %d of %d in single block\n”,
tid, nthreads);
}

#pragma omp master
{
fprintf(stderr, “thread %d of %d in master block\n”,
tid, nthreads);
}

}

return 0;
}

#include
#include
#include

int main(int argc, char **argv) {

/* All variables declared outside a parallel region
are shared in the parallel region */
int x = 7;
int y = 34;
int *A;

#pragma omp parallel num_threads(4) private(x)
{
/* all variables declared inside a parallel region
are private */
int tid, nthreads;

tid = omp_get_thread_num();
nthreads = omp_get_num_threads();

if (tid == 0) {
x = 0;
y = 3;
}

if (tid == 1) {
x = 1;
y = 2;
}

printf(“tid %d, x %d, y %d\n”, tid, x, y);

#pragma omp barrier

}

printf(“outside the loop, x %d y %d\n”, x, y);

return 0;
}

#include
#include
#include
#include

int main(int argc, char **argv) {

/* create two arrays of size 1 million each */
int *A;
A = (int *) malloc(1000000 * sizeof(int));
assert(A != NULL);
int *B;
B = (int *) malloc(1000000 * sizeof(int));
assert(B != NULL);

int j;
for (j=0; j<1000000; j++) { A[j] = (j % 8); } #pragma omp parallel num_threads(4) { int i; #pragma omp sections { #pragma omp section { int sumA = 0; for (i=0; i<1000000; i++) { sumA += A[i]; } printf("tid %d, nthreads %d, sum %d\n", omp_get_thread_num(), omp_get_num_threads(), sumA); } #pragma omp section printf("tid %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads()); } } free(A); free(B); return 0; } #include
#include
#include
#include
#include
#ifdef _OPENMP
#include
#endif

static double timer() {
/*
struct timeval tp;
gettimeofday(&tp, NULL);
return ((double) (tp.tv_sec) + 1e-6 * tp.tv_usec);
*/

/* The code below is for another high resolution timer */
/* I’m using gettimeofday because it’s more portable */

struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
return ((double) (tp.tv_sec) + 1e-9 * tp.tv_nsec);

}

static int sum_serial(const int *A, const int n, const int num_iterations) {

fprintf(stderr, “N %d\n”, n);
fprintf(stderr, “Variant: serial\n”);
fprintf(stderr, “Execution times (ms) for %d iterations:\n”, num_iterations);

int iter;
double avg_elt;

avg_elt = 0.0;

for (iter = 0; iter < num_iterations; iter++) { double elt; elt = timer(); int sum = 0; int i; for (i=0; i\n”, argv[0]);
exit(1);
}

int n;

n = atoi(argv[1]);

assert(n > 0);
assert(n <= 1000000000); /* making n a multiple of 4 */ n = (n/4) * 4; int *A; A = (int *) malloc(n * sizeof(int)); assert(A != 0); int i; /* Why are we using 'parallel for' here? */ /* initialize values to be 0, 1, 2, 3 */ #pragma omp parallel for private(i) for (i=0; i
#include
#include

void some_other_func(int);

static void static_func(int i) {
printf(“hello from static_func %d\n”, i);
}

int main(int argc, char **argv) {

int x = 0;
if (argc == 2)
x = atoi(argv[1]);

int y = 2;

if (x > 0) {

#pragma omp parallel if ((x == 4) && (y == 2))
{
int tid, nthreads;

tid = omp_get_thread_num();
nthreads = omp_get_num_threads();

static_func(tid);
some_other_func(tid);

printf(“Hello world from thread %3d of %3d\n”, tid, nthreads);

//#pragma omp barrier

}

}

return 0;
}

critical_test.c

fib_test.c

for_test.c

nested_test.c

ordered_test.c

other.c

other_test.c

private_shared.c

section_test.c

sum_omp.c

test.c