Exemplo n.º 1
0
int main (int argc, char **argv)
{
    FILE *file;
    int i;
    int loopnum;
    fptype * buffer;
    int * buffer2;
    int rv;

/* #ifdef PARSEC_VERSION */
/* #define __PARSEC_STRING(x) #x */
/* #define __PARSEC_XSTRING(x) __PARSEC_STRING(x) */
/*         printf("PARSEC Benchmark Suite Version "__PARSEC_XSTRING(PARSEC_VERSION)"\n"); */
/* 	fflush(NULL); */
/* #else */
/*         printf("PARSEC Benchmark Suite\n"); */
/* 	fflush(NULL); */
/* #endif //PARSEC_VERSION */
/* #ifdef ENABLE_PARSEC_HOOKS */
/*    __parsec_bench_begin(__parsec_blackscholes); */
/* #endif */

   /* if (argc != 4) */
   /*      { */
   /*              printf("Usage:\n\t%s <nthreads> <inputFile> <outputFile>\n", argv[0]); */
   /*              exit(1); */
   /*      } */
    nThreads = 1; /* atoi(argv[1]); */
    /* char *inputFile = argv[2]; */
    /* char *outputFile = argv[3]; */

    //Read input data from file
    /* file = fopen(inputFile, "r"); */
    /* if(file == NULL) { */
    /*   printf("ERROR: Unable to open file `%s'.\n", inputFile); */
    /*   exit(1); */
    /* } */
    rv = /* f */scanf(/* file, */ "%i", &numOptions);
    if(rv != 1) {
      /* printf("ERROR: Unable to read from file `%s'.\n", inputFile); */
      printf("ERROR: unable to read from stdin\n");
      /* fclose(file); */
      exit(1);
    }
    /* if(nThreads > numOptions) { */
    /*   printf("WARNING: Not enough work, reducing number of threads to match number of options.\n"); */
    /*   nThreads = numOptions; */
    /* } */

/* #if !defined(ENABLE_THREADS) && !defined(ENABLE_OPENMP) && !defined(ENABLE_TBB) */
    if(nThreads != 1) {
        printf("Error: <nthreads> must be 1 (serial version)\n");
        exit(1);
    }
/* #endif */

    // alloc spaces for the option data
    data = (OptionData*)malloc(numOptions*sizeof(OptionData));
    prices = (fptype*)malloc(numOptions*sizeof(fptype));
    for ( loopnum = 0; loopnum < numOptions; ++ loopnum )
    {
        rv = /* f */scanf(/* file, */
			  "%f %f %f %f %f %f %c %f %f",
			  &data[loopnum].s,
			  &data[loopnum].strike,
			  &data[loopnum].r,
			  &data[loopnum].divq,
			  &data[loopnum].v,
			  &data[loopnum].t,
			  &data[loopnum].OptionType,
			  &data[loopnum].divs,
			  &data[loopnum].DGrefval);
        if(rv != 9) {
          /* printf("ERROR: Unable to read from file `%s'.\n", inputFile); */
          /* fclose(file); */
	  printf("Error: Unable to read from stdin\n");
	  exit(1);
        }
    }
    /* rv = fclose(file); */
    /* if(rv != 0) { */
    /*   printf("ERROR: Unable to close file `%s'.\n", inputFile); */
    /*   exit(1); */
    /* } */

/* #ifdef ENABLE_THREADS */
/*     MAIN_INITENV(,8000000,nThreads); */
/* #endif */
    printf("Num of Options: %d\n", numOptions);
    printf("Num of Runs: %d\n", NUM_RUNS);

#define PAD 256
#define LINESIZE 64

    buffer = (fptype *) malloc(5 * numOptions * sizeof(fptype) + PAD);
    sptprice = (fptype *) (((unsigned long long)buffer + PAD) & ~(LINESIZE - 1));
    strike = sptprice + numOptions;
    rate = strike + numOptions;
    volatility = rate + numOptions;
    otime = volatility + numOptions;

    buffer2 = (int *) malloc(numOptions * sizeof(fptype) + PAD);
    otype = (int *) (((unsigned long long)buffer2 + PAD) & ~(LINESIZE - 1));

    for (i=0; i<numOptions; i++) {
        otype[i]      = (data[i].OptionType == 'P') ? 1 : 0;
        sptprice[i]   = data[i].s;
        strike[i]     = data[i].strike;
        rate[i]       = data[i].r;
        volatility[i] = data[i].v;    
        otime[i]      = data[i].t;
    }

    printf("Size of data: %d\n", numOptions * (sizeof(OptionData) + sizeof(int)));

/* #ifdef ENABLE_PARSEC_HOOKS */
/*     __parsec_roi_begin(); */
/* #endif */

/* #ifdef ENABLE_THREADS */
/* #ifdef WIN32 */
/*     HANDLE *threads; */
/*     int *nums; */
/*     threads = (HANDLE *) malloc (nThreads * sizeof(HANDLE)); */
/*     nums = (int *) malloc (nThreads * sizeof(int)); */

/*     for(i=0; i<nThreads; i++) { */
/*         nums[i] = i; */
/*         threads[i] = CreateThread(0, 0, bs_thread, &nums[i], 0, 0); */
/*     } */
/*     WaitForMultipleObjects(nThreads, threads, TRUE, INFINITE); */
/*     free(threads); */
/*     free(nums); */
/* #else */
/*     int *tids; */
/*     tids = (int *) malloc (nThreads * sizeof(int)); */

/*     for(i=0; i<nThreads; i++) { */
/*         tids[i]=i; */
/*         CREATE_WITH_ARG(bs_thread, &tids[i]); */
/*     } */
/*     WAIT_FOR_END(nThreads); */
/*     free(tids); */
/* #endif //WIN32 */
/* #else //ENABLE_THREADS */
/* #ifdef ENABLE_OPENMP */
/*     { */
/*         int tid=0; */
/*         omp_set_num_threads(nThreads); */
/*         bs_thread(&tid); */
/*     } */
/* #else //ENABLE_OPENMP */
/* #ifdef ENABLE_TBB */
/*     tbb::task_scheduler_init init(nThreads); */

/*     int tid=0; */
/*     bs_thread(&tid); */
/* #else //ENABLE_TBB */
    //serial version
    int tid=0;
    bs_thread(&tid);
/* #endif //ENABLE_TBB */
/* #endif //ENABLE_OPENMP */
/* #endif //ENABLE_THREADS */

/* #ifdef ENABLE_PARSEC_HOOKS */
/*     __parsec_roi_end(); */
/* #endif */

    //Write prices to output file
    /* file = fopen(outputFile, "w"); */
    /* if(file == NULL) { */
    /*   printf("ERROR: Unable to open file `%s'.\n", outputFile); */
    /*   exit(1); */
    /* } */
    rv = /* f */printf(/* file,  */"%i\n", numOptions);
    if(rv < 0) {
      /* printf("ERROR: Unable to write to file `%s'.\n", outputFile); */
      /* fclose(file); */
      printf("ERROR: Unable to write to stdout\n");
      exit(1);
    }
    for(i=0; i<numOptions; i++) {
      rv = /* f */printf(/* file,  */"%.18f\n", prices[i]);
      if(rv < 0) {
        /* printf("ERROR: Unable to write to file `%s'.\n", outputFile); */
        /* fclose(file); */
	printf("ERROR: Unable to write to stdout\n");
	exit(1);
      }
    }
    /* rv = fclose(file); */
    /* if(rv != 0) { */
    /*   printf("ERROR: Unable to close file `%s'.\n", outputFile); */
    /*   exit(1); */
    /* } */

#ifdef ERR_CHK
    printf("Num Errors: %d\n", numError);
#endif
    free(data);
    free(prices);

/* #ifdef ENABLE_PARSEC_HOOKS */
/*     __parsec_bench_end(); */
/* #endif */

    return 0;
}
int main(int argc, char **argv) {
  FILE *file;
  int i;
  int loopnum;
  fptype * buffer;
  int * buffer2;
  int rv;

  printf("PARSEC Benchmark Suite\n");

  if(argc != 4) {
    printf("Usage:\n\t%s <nthreads> <inputFile> <outputFile>\n", argv[0]);
    exit(1);
  }
  nThreads = atoi(argv[1]);
  char *inputFile = argv[2];
  char *outputFile = argv[3];

  //Read input data from file
  file = fopen(inputFile, "r");
  if(file == NULL) {
    printf("ERROR: Unable to open file `%s'.\n", inputFile);
    exit(1);
  }
  rv = fscanf(file, "%i", &numOptions);
  if(rv != 1) {
    printf("ERROR: Unable to read from file `%s'.\n", inputFile);
    fclose(file);
    exit(1);
  }
  if(nThreads > numOptions) {
    printf("WARNING: Not enough work, reducing number of threads to match number of options.\n");
    nThreads = numOptions;
  }

  // alloc spaces for the option data
  data = (OptionData*)malloc( numOptions * sizeof(OptionData) );
  prices = (fptype*)malloc( numOptions * sizeof(fptype) );
  for(loopnum = 0; loopnum < numOptions; ++loopnum) {
    rv =
      fscanf(file,
             "%f %f %f %f %f %f %c %f %f",
             &data[loopnum].s,
             &data[loopnum].strike,
             &data[loopnum].r,
             &data[loopnum].divq,
             &data[loopnum].v,
             &data[loopnum].t,
             &data[loopnum].OptionType,
             &data[loopnum].divs,
             &data[loopnum].DGrefval);
    if(rv != 9) {
      printf("ERROR: Unable to read from file `%s'.\n", inputFile);
      fclose(file);
      exit(1);
    }
  }
  rv = fclose(file);
  if(rv != 0) {
    printf("ERROR: Unable to close file `%s'.\n", inputFile);
    exit(1);
  }

  printf("Num of Options: %d\n", numOptions);
  printf("Num of Runs: %d\n", NUM_RUNS);

#define PAD 256
#define LINESIZE 64

  buffer = (fptype *) malloc(5 * numOptions * sizeof(fptype) + PAD);
  sptprice = (fptype *) ( ( (unsigned long long)buffer + PAD ) & ~(LINESIZE - 1) );
  strike = sptprice + numOptions;
  rate = strike + numOptions;
  volatility = rate + numOptions;
  otime = volatility + numOptions;

  buffer2 = (int *) malloc(numOptions * sizeof(fptype) + PAD);
  otype = (int *) ( ( (unsigned long long)buffer2 + PAD ) & ~(LINESIZE - 1) );

  for(i = 0; i < numOptions; i++) {
    otype[i]      = (data[i].OptionType == 'P') ? 1 : 0;
    sptprice[i]   = data[i].s;
    strike[i]     = data[i].strike;
    rate[i]       = data[i].r;
    volatility[i] = data[i].v;
    otime[i]      = data[i].t;
  }

  printf( "Size of data: %d\n", numOptions * ( sizeof(OptionData) + sizeof(int) ) );

  // Run the program in parallel
  int tid = 0;
  omp_set_num_threads(nThreads);
  double start_time = omp_get_wtime();
  bs_thread(&tid);
  double run_time = omp_get_wtime() - start_time;
  printf("Completed in %f seconds \n", run_time);

  //Write prices to output file
  file = fopen(outputFile, "w");
  if(file == NULL) {
    printf("ERROR: Unable to open file `%s'.\n", outputFile);
    exit(1);
  }
  rv = fprintf(file, "%i\n", numOptions);
  if(rv < 0) {
    printf("ERROR: Unable to write to file `%s'.\n", outputFile);
    fclose(file);
    exit(1);
  }
  for(i = 0; i < numOptions; i++) {
    rv = fprintf(file, "%.18f\n", prices[i]);
    if(rv < 0) {
      printf("ERROR: Unable to write to file `%s'.\n", outputFile);
      fclose(file);
      exit(1);
    }
  }
  rv = fclose(file);
  if(rv != 0) {
    printf("ERROR: Unable to close file `%s'.\n", outputFile);
    exit(1);
  }

#ifdef ERR_CHK
  printf("Num Errors: %d\n", numError);
#endif

  free(data);
  free(prices);

  return 0;
}
Exemplo n.º 3
0
DWORD WINAPI bs_thread(LPVOID tid_ptr){
#else
int bs_thread(void *tid_ptr) {
#endif
    int i, j;
    fptype price;
    fptype priceDelta;
    int tid = *(int *)tid_ptr;
    int start = tid * (numOptions / nThreads);
    int end = start + (numOptions / nThreads);

    for (j=0; j<NUM_RUNS; j++) {
#ifdef ENABLE_OPENMP
#pragma omp parallel for
        for (i=0; i<numOptions; i++) {
#else  //ENABLE_OPENMP
        for (i=start; i<end; i++) {
#endif //ENABLE_OPENMP
            /* Calling main function to calculate option value based on 
             * Black & Sholes's equation.
             */
            price = BlkSchlsEqEuroNoDiv( sptprice[i], strike[i],
                                         rate[i], volatility[i], otime[i], 
                                         otype[i], 0);
            prices[i] = price;
            
#ifdef ERR_CHK   
            priceDelta = data[i].DGrefval - price;
            if( fabs(priceDelta) >= 1e-4 ){
                printf("Error on %d. Computed=%.5f, Ref=%.5f, Delta=%.5f\n",
                       i, price, data[i].DGrefval, priceDelta);
                numError ++;
            }
#endif
        }
    }

    return 0;
}

int main (int argc, char **argv)
{
    FILE *file;
    int i;
    int loopnum;
    fptype * buffer;
    int * buffer2;
    int rv;

#ifdef PARSEC_VERSION
#define __PARSEC_STRING(x) #x
#define __PARSEC_XSTRING(x) __PARSEC_STRING(x)
        printf("PARSEC Benchmark Suite Version "__PARSEC_XSTRING(PARSEC_VERSION)"\n");
	fflush(NULL);
#else
        printf("PARSEC Benchmark Suite\n");
	fflush(NULL);
#endif //PARSEC_VERSION
#ifdef ENABLE_PARSEC_HOOKS
   __parsec_bench_begin(__parsec_blackscholes);
#endif

   if (argc != 4)
        {
                printf("Usage:\n\t%s <nthreads> <inputFile> <outputFile>\n", argv[0]);
                exit(1);
        }
    nThreads = atoi(argv[1]);
    char *inputFile = argv[2];
    char *outputFile = argv[3];

    //Read input data from file
    file = fopen(inputFile, "r");
    if(file == NULL) {
      printf("ERROR: Unable to open file %s.\n", inputFile);
      exit(1);
    }
    rv = fscanf(file, "%i", &numOptions);
    if(rv != 1) {
      printf("ERROR: Unable to read from file %s.\n", inputFile);
      fclose(file);
      exit(1);
    }
    if(nThreads > numOptions) {
      printf("WARNING: Not enough work, reducing number of threads to match number of options.\n");
      nThreads = numOptions;
    }

#if !defined(ENABLE_THREADS) && !defined(ENABLE_OPENMP)
    if(nThreads != 1) {
        printf("Error: <nthreads> must be 1 (serial version)\n");
        exit(1);
    }
#endif

    // alloc spaces for the option data
    data = (OptionData*)malloc(numOptions*sizeof(OptionData));
    prices = (fptype*)malloc(numOptions*sizeof(fptype));
    for ( loopnum = 0; loopnum < numOptions; ++ loopnum )
    {
        rv = fscanf(file, "%f %f %f %f %f %f %c %f %f", &data[loopnum].s, &data[loopnum].strike, &data[loopnum].r, &data[loopnum].divq, &data[loopnum].v, &data[loopnum].t, &data[loopnum].OptionType, &data[loopnum].divs, &data[loopnum].DGrefval);
        if(rv != 9) {
          printf("ERROR: Unable to read from file %s with loopnum %d.\n", inputFile, loopnum);
          fclose(file);
          exit(1);
        }
    }
    rv = fclose(file);
    if(rv != 0) {
      printf("ERROR: Unable to close file %s.\n", inputFile);
      exit(1);
    }

#ifdef ENABLE_THREADS
    
//    pthread_mutexattr_init( &_M4_normalMutexAttr);
//    pthread_mutexattr_settype( &_M4_normalMutexAttr, PTHREAD_MUTEX_NORMAL);
    _M4_numThreads = nThreads;
    {
        int _M4_i;
        for ( _M4_i = 0; _M4_i < MAX_THREADS; _M4_i++) {
            _M4_threadsTable[_M4_i] = -1;
        }
    }
;
#endif
    printf("Num of Options: %d\n", numOptions);
    printf("Num of Runs: %d\n", NUM_RUNS);

#define PAD 256
#define LINESIZE 64

    buffer = (fptype *) malloc(5 * numOptions * sizeof(fptype) + PAD);
    sptprice = (fptype *) (((unsigned long long)buffer + PAD) & ~(LINESIZE - 1));
    strike = sptprice + numOptions;
    rate = strike + numOptions;
    volatility = rate + numOptions;
    otime = volatility + numOptions;

    buffer2 = (int *) malloc(numOptions * sizeof(fptype) + PAD);
    otype = (int *) (((unsigned long long)buffer2 + PAD) & ~(LINESIZE - 1));

    for (i=0; i<numOptions; i++) {
        otype[i]      = (data[i].OptionType == 'P') ? 1 : 0;
        sptprice[i]   = data[i].s;
        strike[i]     = data[i].strike;
        rate[i]       = data[i].r;
        volatility[i] = data[i].v;    
        otime[i]      = data[i].t;
    }

    printf("Size of data: %d\n", numOptions * (sizeof(OptionData) + sizeof(int)));

#ifdef ENABLE_PARSEC_HOOKS
    __parsec_roi_begin();
#endif
#ifdef ENABLE_THREADS
    int tids[nThreads];
	pthread_t thread_table[nThreads];
    for(i=0; i<nThreads; i++) {
        tids[i]=i;
    }
    for(i=0; i<nThreads; i++) { 
//		fprintf(stderr, "create %d thread\n", i);
		pthread_create(&thread_table[i],NULL,(void *(*)(void *))bs_thread,(void *)&tids[i]);
    }
    for(i=0; i<nThreads; i++) {
		pthread_join(thread_table[i], NULL);
	}
#else//ENABLE_THREADS
#ifdef ENABLE_OPENMP
    {
        int tid=0;
        omp_set_num_threads(nThreads);
        bs_thread(&tid);
    }
#else //ENABLE_OPENMP
#ifdef WIN32 
    if (nThreads > 1)
    {
        HANDLE threads[MAX_THREADS];
                int nums[MAX_THREADS];
                for(i=0; i<nThreads; i++) {
                        nums[i] = i;
                        threads[i] = CreateThread(0, 0, bs_thread, &nums[i], 0, 0);
                }
                WaitForMultipleObjects(nThreads, threads, TRUE, INFINITE);
    } else
#endif
    {
        int tid=0;
        bs_thread(&tid);
    }
#endif //ENABLE_OPENMP
#endif //ENABLE_THREADS
#ifdef ENABLE_PARSEC_HOOKS
    __parsec_roi_end();
#endif

    //Write prices to output file
    file = fopen(outputFile, "w");
    if(file == NULL) {
      printf("ERROR: Unable to open file %s.\n", outputFile);
      exit(1);
    }
    rv = fprintf(file, "%i\n", numOptions);
    if(rv < 0) {
      printf("ERROR: Unable to write to file %s.\n", outputFile);
      fclose(file);
      exit(1);
    }
    for(i=0; i<numOptions; i++) {
      rv = fprintf(file, "%.18f\n", prices[i]);
      if(rv < 0) {
        printf("ERROR: Unable to write to file %s.\n", outputFile);
        fclose(file);
        exit(1);
      }
    }
    rv = fclose(file);
    if(rv != 0) {
      printf("ERROR: Unable to close file %s.\n", outputFile);
      exit(1);
    }

#ifdef ERR_CHK
    printf("Num Errors: %d\n", numError);
#endif
    free(data);
    free(prices);

#ifdef ENABLE_PARSEC_HOOKS
    __parsec_bench_end();
#endif

    return 0;
}