Cè¨èªã§ä¸¦åå¦ç(OpenMP)
Windows(minGW), Mac, Linux ã®gcc(version 4.2以é)ã§åä½ç¢ºèªæ¸ã æ°ããã©ã¤ãã©ãªçãã¤ã³ã¹ãã¼ã«ãããã¨ãªããããã©ã«ãã®ã¾ã¾ã®gccã§ã³ã³ãã¤ã«ãéã£ãã ãããªã«æ軽ã«ä¸¦åè¨ç®ã§ãããªãã¦ãããã
ã½ã¼ã¹ã³ã¼ã
// hoge.c // 並åå¦çã®ããã°ã©ã // Windows, Mac, Linux ã®å ¨ã¦ã® gcc (4.2以é) ã§æ¬ããã°ã©ã åä½ç¢ºèªæ¸ #include <stdio.h> #include <omp.h> int main(int argc, char const* argv[]) { int n; n = omp_get_max_threads(); // ããã©ã«ãã®ã¹ã¬ããæ°ãåå¾ printf("max threads (default): %d\n",n); omp_set_num_threads(16); // ã¹ã¬ããæ°ãå¤æ´ n = omp_get_max_threads(); // ã¹ã¬ããæ°ãååå¾ printf("max threads (set): %d\n",n); // 並åå¦ç é åarray[i]ã« i ãå ¥ãã int a[10], i; #pragma omp parallel for for(i = 0; i < 10; i++) { a[i] = i; printf("parallel: %d\n", i); } // é åã«æ£ããå ¥ã£ãã表示 printf("a[0]:%d a[1]:%d a[2]:%d a[9]:%d\n", a[0],a[1],a[2],a[9]); return 0; }
ã³ã³ãã¤ã«
bash-3.2$ gcc -fopenmp -O2 hoge.c
å®è¡çµæ
bash-3.2$ ./a.out max threads (default): 4 max threads (set): 16 parallel: 1 parallel: 5 parallel: 7 parallel: 9 parallel: 3 parallel: 2 parallel: 4 parallel: 6 parallel: 8 parallel: 0 a[0]:0 a[1]:1 a[2]:2 a[9]:9
åè: OpenMPè¦ãæ¸ã