c和c++ 使用openmp的对比

| | Comments (0)
系统 : 2 x Xeon 5130 2.00GHz 1333MHz FSB (4 cores) - Woodcrest B2, 64-bit, dual-core, 65nm, L2: 4MB

testc.c:
    1 #include <omp.h>
    2 #include <stdio.h>
    3
    4 int main()
    5 {
    6     int i = 0;
    7     int j = 0;
    8     fprintf(stderr,"omp max: %d\n",omp_get_max_threads());
    9     omp_set_num_threads(4);
   10    
   11 #pragma omp parallel for
   12     for(i = 0; i< 10;i++)
   13     {
   14         fprintf(stderr,"omp get: (%d) %d\n",i,omp_get_num_threads());
   15         for (j = 0;j<10000;j++)
   16             ;
   17         fprintf(stderr,"%d\n",i);
   18     }  
   19     return 0;
   20 }  

testcpp.cpp
    1 #include <omp.h>
    2 #include <iostream>
    3
    4 using namespace std;
    5
    6 int main()
    7 {
    8     int i = 0;
    9     int j = 0;
   10     cerr << "omp max: " << omp_get_max_threads() << endl;
   11     omp_set_num_threads(4);
   12
   13 #pragma omp parallel for
   14     for(i = 0; i< 10;i++)
   15     {
   16         cerr << "omp get: (" << i << ") " <<  omp_get_num_threads() << endl;
   17         for (j = 0;j<10000;j++)
   18             ;
   19         cerr << i << endl;
   20     }
   21     return 0;
   22 }

Makefile:
    1 all:
    2     g++42 -o testcpp testcpp.cpp -lgomp -lpthread -fopenmp
    3     gcc42 -o testc testc.c -lgomp -lpthread -fopenmp

两段程序几乎一样,除了对信息的输出对于结果的现实却相差很远:
[~workspace]$ time ./testc
omp max: 4
omp get: (3) 4
omp get: (0) 4
omp get: (6) 4
omp get: (9) 4
0
3
6
omp get: (1) 4
omp get: (4) 4
9
omp get: (7) 4
1
omp get: (2) 4
4
2
7
omp get: (5) 4
omp get: (8) 4
8
5

real    0m0.003s
user    0m0.003s
sys     0m0.001s

而:
[~workspace]$ time ./testcpp
omp max: 4
omp get: (omp get: (omp get: (omp get: (0693) ) ) ) 4444



06

omp get: (9omp get: (317

) ) 4omp get: (4
4
) 14

7
omp get: (42omp get: (
) 8omp get: (4) 5
4)
42

8
5

real    0m0.008s
user    0m0.004s
sys     0m0.007s


看来openmp对c的支持要更规矩一些。






Leave a comment

About this Entry

This page contains a single entry by susnbsd published on January 11, 2008 1:29 PM.

OpenMP 与 游戏 was the previous entry in this blog.

Find recent content on the main index or look in the archives to find all content.

MyFriends