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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#ifdef NULIX
#include <pthread.h>
#include <iostream>
#ifdef USE_TBB
#include <tbb/scalable_allocator.h>
#include <tbb/cache_aligned_allocator.h>
#endif
#endif
#include <stdio.h>
#include <time.h>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <time.h>
#ifdef MULTITHREAD
#define NBCPU 2
#else
#define NBCPU 1
#endif
#define T 1000000
#define N 200
using namespace std;
#ifdef USE_TBB
typedef std::vector< long int , tbb::scalable_allocator<long int> > myvector;
#else
typedef std::vector< long int > myvector;
#endif
int test1=0,test2=0;
myvector a1,b1,c;
myvector a2,b2,d;
//------------------------------------------------------------------------------------------------------------------------
void* thread1(void*)
{
long int cptT,cptI , i;
cout<<"Thread1!\n";
for (cptT=0;cptT<T;cptT++)
{
c.clear();
myvector::iterator it1,it2;
for(it1=a1.begin(),it2=b1.begin();it1!=a1.end(),it2!=b1.end();it1++,it2++)
c.push_back((*it1)+(*it2));
}
cout<<"Verification de C: \n";
for (myvector::iterator it=c.begin();it!=c.end();it++) if (*it!=0) test1=11;
cout<<"Fin thread 1.\n";
return NULL;
}
//------------------------------------------------------------------------------------------------------------------------
void* thread2(void*)
{
long int cptT,cptI , i;
cout<<"Thread2!\n";
for (cptT=0;cptT<T;cptT++)
{
d.clear();
myvector::iterator it1,it2;
for(it1=a2.begin(),it2=b2.begin(); it1!=a2.end(),it2!=b2.end(); it1++,it2++)
d.push_back((*it1)+(*it2));
}
cout<<"Verification de D: \n";
for (myvector::iterator it=d.begin();it!=d.end();it++) if (*it!=0) test2=22;
cout<<"Fin thread 2.\n";
return NULL;
}
//------------------------------------------------------------------------------------------------------------------------
int main( )
{
clock_t debut,fin;
long int i;
for (i=0;i<N;i++) { a1.push_back(i); b1.push_back(i); }
for (i=0;i<N;i++) { a2.push_back(i); b2.push_back(i); }
debut=clock();
pthread_t t1,t2;
pthread_create(&t1,NULL,thread1,NULL);
#ifdef MULTITHREAD
pthread_create(&t2,NULL,thread2,NULL);
#endif
pthread_join(t1,NULL);
#ifdef MULTITHREAD
pthread_join(t2,NULL);
#endif
fin=clock();
cout<<"Temps : "<<(fin-debut)/1000/NBCPU<<" millisecondes"<<endl;
cout <<"Test1 : "<<test1<<" test2 : "<<test2<<"\n";;
cout<<endl<<endl;
return 0;
} |
Partager