Strumenti Utente

Strumenti Sito


magistraleinformaticanetworking:spm:spm1213_c_omp_fused
fused.cpp
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include "omp.h"
#include <ctime>
 
using namespace std; 
 
float f(float x, long n) {
  for(long i=0; i<n; i++) 
    x = sin(x); 
  return x; 
}
 
int main(int argc, char * argv[]) {
 
  if(argc!=4) {
    cout <<  "Usage is:\n" << argv[0] << " iterno n nw " << endl; 
    return(0);
  }
 
  long iter = atoi(argv[1]); 
  long n    = atoi(argv[2]);
  long nw   = atoi(argv[3]); 
 
  float * x = new float[n];
  for(int i=0; i<n; i++) 
    x[i] = ((float) rand())/((float) RAND_MAX);
 
  struct timespec t0, t1;
  clock_gettime(CLOCK_THREAD_CPUTIME_ID,&t0);
#pragma omp parallel for num_threads(nw), private(i)
  for(int i=0; i<n; i++) {
    x[i] = f(f(x[i],n),n);
  }
  clock_gettime(CLOCK_THREAD_CPUTIME_ID,&t1);
 
 
  cout << "Elapsed time " << t1.tv_nsec - t0.tv_nsec << " (" << t1.tv_sec - t0.tv_sec << ")" << endl; 
  return(x[rand() % n]);
}
magistraleinformaticanetworking/spm/spm1213_c_omp_fused.txt · Ultima modifica: 30/04/2013 alle 16:55 (6 anni fa) da Marco Danelutto