magistraleinformaticanetworking:spm:spm1213_c_omp_load_balancing
- sbil_omp.cpp
#include <iostream> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <omp.h> using namespace std; float **a; float calc(float x) { long iter = (long) x; for(long i=0; i<iter; i++) x = sin(x); return(x); } int main(int argc, char * argv[]) { if(argc == 1) { cout << "Usage is:\n" << argv[0] << " veclen numthread " << endl; return(0); } int n = atoi(argv[1]); int tn1 = atoi(argv[2]); int tn2 = atoi(argv[3]); float * tim = new float[tn2-tn1+1]; for(int k=tn1; k<=tn2; k++) { a = new float * [n]; for(int i=0; i<n; i++) a[i] = new float[n]; for(int i=0; i<n; i++) for(int j=0; j<n; j++) a[i][j] = i* 100 + j * 10; float sum = 0.0; float ts0 = omp_get_wtime(); #pragma omp parallel for num_threads(k) schedule(SCHED) reduction (+:sum) for(int i=0; i<n; i++) for(int j=0; j<n; j++) { a[i][j] = calc(a[i][j]); sum += a[i][j]; } float ts1 = omp_get_wtime(); tim[k-1] = ts1 - ts0; } for(int i=0; i<tn2-tn1+1; i++) { cout << "nt = " << i+1 << "\t tc = " << tim[i] << "\t sp = " << tim[0] / tim[i] << "\t eff = " << (tim[0] / ( (i+1) * tim[i] )) << endl; } return(0); }
magistraleinformaticanetworking/spm/spm1213_c_omp_load_balancing.txt · Ultima modifica: 22/04/2013 alle 16:17 (12 anni fa) da Marco Danelutto