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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
/* -*- c-file-style: "GNU" -*- */
/*
* Copyright (C) CNRS, INRIA, Université Bordeaux 1, Télécom SudParis
* See COPYING in top-level directory.
*/
# include <stdlib.h>
# include <stdio.h>
# include <time.h>
# include <omp.h>
#include <stdarg.h>
// Debugging part, print out only if debugging level of the system is verbose or more
int _debug = -77;
void debug(char *fmt, ...) {
if (_debug == -77) {
char *buf = getenv("EZTRACE_DEBUG");
if (buf == NULL)
_debug = 0;
else
_debug = atoi(buf);
}
if (_debug >= 0) { // debug verbose mode
va_list va;
va_start(va, fmt);
vfprintf(stdout, fmt, va);
va_end(va);
}
}
// end of debugging part
#define SIZE (100)
_Atomic int res = 0;
omp_lock_t lock;
void task_function(int n) {
// printf("task_function(%d) : res = %d\n", n, res);
if (n % 2) {
omp_set_lock(&lock);
res += 1;
omp_unset_lock(&lock);
}
}
int main(void) {
omp_init_lock(&lock);
omp_set_num_threads(4);
int j;
#pragma omp parallel for
for (j = 0; j < 10; j++) {
debug("loop %d\n", j);
for (int i = 0; i < SIZE; i++) {
if (j % 2) {
#pragma omp task untied
task_function(i);
} else {
#pragma omp task
task_function(i);
}
}
#pragma omp taskwait
}
debug("result = %d\n", res);
return 0;
}
|