Beispiel #1
0
int main(int argc, char **argv) {


	struct timeval stop;
	struct timeval start;

	int size = 2048;
	int iter = 100;
	__cilkrts_set_param("nworkers", argv[1]);
	double *coeff = (double*) malloc (size * size * sizeof(double));
	double *approx = (double*) malloc (size * sizeof(double));
	double *b = (double*) malloc (size * sizeof(double));

	srand(time(NULL));
	for(int i = 0; i < size; i++) {
		for(int j = 0; j < size; j++) {
			coeff[i * size + j] = (double) ((rand() % size) + 1) - size/2;		
		}
		approx[i] = (double) ((rand() % 10) + 1) ;
		b[i] = (double) ((rand() % 10) + 1);
	}
	gettimeofday(&start, NULL); 
	double *inv = diagonalInverse(coeff, size);
	double *r = LUMatrix(coeff, size);


	double *res,*temp;
	for (int ct = 0; ct < iter; ct++) {

		res = matrixVect(r, approx, size);
		temp = (double*) malloc (size * sizeof(double));

		cilk_for(int i = 0; i < size; i++)
			temp[i] = b[i] - res[i];

		res = matrixVect(inv, temp, size);

		cilk_for(int i = 0; i < size; i++) 
			approx[i] = res[i];

		printf("The Value after iteration %d is\n",ct);
        for(int i = 0; i<size; i++)
            printf("%.3f\n",approx[i]);

        free(res);
        free(temp);

	}
	gettimeofday(&stop, NULL); 
	printf("\n\nComputing time: %lu s - %d us\n",stop.tv_sec - start.tv_sec,stop.tv_usec - start.tv_usec);


	free(coeff);
	free(approx);
	free(b);
	free(inv);
	free(r);

	return 0;
}
Beispiel #2
0
int cilk_main(
  std::string _inputname,
  std::string _coloringAlgorithm,
  std::string _numWorkers,
  std::string _ordering) {
  __cilkrts_set_param("nworkers", _numWorkers.c_str());

  sparseRowMajor<int, int> inputGraph;
  // Parse the graph in .mtx or adjacency list representation.
  if (_inputname.find(".mtx") != string::npos) {
    graph<int> inter = graphFromEdges<int>(edgesFromMtxFile<int>(_inputname.c_str()), true);
    inputGraph = sparseFromGraph<int>(inter);
  } else if (_inputname.find(".edges") != string::npos) {
    graph<int> inter = graphFromEdges<int>(readEdgeArrayFromFile<int>((char*)_inputname.c_str()), true);
    inputGraph = sparseFromGraph<int>(inter);
  } else {
    graph<int> inter = graphFromEdges<int>(edgesFromGraph<int>(readGraphFromFile<int>((char*)_inputname.c_str())), true);
    inputGraph = sparseFromGraph<int>(inter);
  }


  // Initialize the vertex color array, and the permutation.
  // int* vertexColors = (int*) malloc(sizeof(int)*inputGraph.numRows);
  if (inputGraph.Values == NULL) {
    inputGraph.Values = (int*) malloc(sizeof(int)*inputGraph.numRows);
  }
  int* vertexColors = inputGraph.Values;

  unsigned int seed = 1234134;
  double beginTime = getTime();
  int numColors;
  if (_coloringAlgorithm.compare("JP") == 0) {
    numColors = colorGraphJP(&inputGraph, vertexColors, _ordering, seed);
  } else {
    printf("Invalid coloring algorithm\n");
  }
  double endTime = getTime();

  for (int i = 0; i < inputGraph.numRows; i++) {
    for (int j = 0; j < getDegree(&inputGraph, i); j++) {
      int nbr = getNeighbor(&inputGraph, i, j);
      if (vertexColors[i] == vertexColors[nbr]) {
        printf("Error: vertex %d and vertex %d are neighbors and share color %d\n", i, nbr, vertexColors[i]);
        return -1;
      }
    }
  }
  printf("Total colors: %d\n", numColors);
  printf("Total time: %f\n", endTime - beginTime);

  return 0;
}
CollisionWorld* CollisionWorld_new(const unsigned int capacity) {
  assert(capacity > 0);
  __cilkrts_set_param("nworkers","8");
  CollisionWorld* collisionWorld = malloc(sizeof(CollisionWorld));
  if (collisionWorld == NULL) {
    return NULL;
  }

  collisionWorld->numLineWallCollisions = 0;
  collisionWorld->numLineLineCollisions = 0;
  collisionWorld->lines = malloc(capacity * sizeof(Line*));
  collisionWorld->numOfLines = 0;
  return collisionWorld;
}
int main(int argc, char *argv[])
{
  int n, result;

  if (argc != 2 and argc != 4) { show_usage(argv[0]); return 1; }
  if (argc == 4)
    {
      if (std::string(argv[1]) != "-n")  { show_usage(argv[0]); return 1; }
      std::string nproc = std::string(argv[2]);
      if (__cilkrts_set_param("nworkers", nproc.c_str()) != __CILKRTS_SET_PARAM_SUCCESS)
	std::cerr << "Failed to set the number of Cilk workers" << std::endl;
    }
  n = atoi(argv[argc - 1]);
  result = fib(n);

  std::cout << "Result: " << result << std::endl;
  return 0;
}
int main(int argc, char** argv){
	double x;
	double y;
	long long pontos;
	int numThreads;
	long long int i;
	struct timeval ini, fim;
	long long unsigned duracao;
	unsigned id;

	cilk::reducer_opadd<long long int> cont;

	gettimeofday(&ini,NULL);

	if(argc!=3){
		printf("Dois parametros requeridos.\n");
		printf("Use ./program <numero de threads> <numero de pontos>\n");
		return(0);
	}
	srand(1337);
	numThreads = strtol(argv[1],NULL,10);
	pontos = strtol(argv[2],NULL,10);

        __cilkrts_end_cilk();  
	__cilkrts_set_param("nworkers", argv[1]);

	gettimeofday(&ini,NULL);

	cilk_for(int i=0; i<pontos; i++){
                x = rand_r(&id)/(double)RAND_MAX;
                y = rand_r(&id)/(double)RAND_MAX;
		if(x*x + y*y <= 1){
			cont = cont + 1;
		}
	}
        gettimeofday(&fim,NULL);
        duracao = (long long unsigned)((fim.tv_sec*1000000 + fim.tv_usec)-(ini.tv_sec*1000000 + ini.tv_usec));
//	printf("%f\n",(double)4*cont.get_value()/(double)pontos);
	printf("%lld\n",duracao);

	return(0);
}
/******************************************************************************
 *   This program calls a version of PRIME_NUMBER that includes
 *   cilk plus directives for parallel processing.
 */
int main ( int argc, char *argv[] )
{
  int n_factor;
  int n_hi;
  int n_lo;

  printf ( "\n\nPRIME_cilklus\n" );
  printf ( "  C/Cilk Plus version\n" );

	if(argc != 3) {
		fprintf(stderr, "Usage: ./executablename <int> num available processors "
						"<int> num threads to run\n\n");
		exit(13);
	}
	
	printf ( "\n" );
	printf ( "  Number of processors available = %i\n", atoi(argv[1]) );
	printf ( "  Number of threads =              %i\n", atoi(argv[2]) );;

	//Set the number of cilk threads to use for this program
	if (0!= __cilkrts_set_param("nworkers", argv[2]))
	{
		printf("Failed to set worker count\n");
		return 1;
	}

  n_lo = 1;
  n_hi = 131072;
  n_factor = 2;
  prime_number_sweep ( n_lo, n_hi, n_factor );

  n_lo = 5;
  n_hi = 500000;
  n_factor = 10;
  prime_number_sweep ( n_lo, n_hi, n_factor );

  printf ( "\nPRIME_cilkplus\n" );
  printf ( "  Normal end of execution.\n\n\n" );

  return 0;
}
Beispiel #7
0
void cilk_set_nb_cores(int proc) {
#ifdef USE_CILK_PLUS
  __cilkrts_set_param("nworkers", std::to_string(proc).c_str());
#endif
}
// A simple test harness 
int inn_prod_driver(int n)
{
  __cilkrts_set_param("nworkers","4");

	double * a = new double[n];
	double * b = new double[n];
	for (int i = 0; i < n; ++i)
	{
        	a[i] = i;
		b[i] = i;
	}
    	std::random_shuffle(a, a + n);
	std::random_shuffle(b, b + n);

	double seqresult = std::inner_product(a, a+n, b, (double)0);	

	long t1 = example_get_time();
	for(int i=0; i< ITERS; ++i)
	{
		seqresult = std::inner_product(a, a+n, b, (double)0);	
	}
	long t2 = example_get_time();

	double seqtime = (t2-t1)/(ITERS*1000.f);
	std::cout << "Sequential time: " << seqtime << " seconds" << std::endl;	
	
	/***********************************************************/
	/********  START TESTING RECURSIVE CILKFIED VERSION  *******/
	/***********************************************************/

	double parresult = rec_cilkified(a, b, n);   
	t1 = example_get_time();
	for(int i=0; i< ITERS; ++i)
	{
		parresult = rec_cilkified(a, b, n);   
	}
 	t2 = example_get_time();

	double partime = (t2-t1)/(ITERS*1000.f);
	std::cout << "Recursive cilkified time:" << partime << " seconds" << std::endl;
	std::cout << "Speedup is: " << seqtime/partime << std::endl;
	std::cout << "Sequential result is: "<<seqresult<<std::endl;
	std::cout << "Recursive cilkified result is: "<<parresult<<std::endl;
	std::cout << "Result is " << (close(seqresult,parresult,n)  ? "correct":"incorrect") << std::endl; 
	
	/****************************************************************/
	/********  START TESTING NESTED LOOPED CILKIFIED VERSION  *******/
	/****************************************************************/
	parresult = loop_cilkified(a, b, n);   
	
	t1 = example_get_time();
	for(int i=0; i< ITERS; ++i)
	{
		//parresult = loop_cilkified(a, b, n);   
 	        parresult = loop_cilkified(a, b, n);   
	}
 	t2 = example_get_time();


	partime = (t2-t1)/(ITERS*1000.f);
	std::cout << "Nested loop cilkified time: " << partime << " seconds" << std::endl;
	std::cout << "Speedup is: " << seqtime/partime << std::endl;
	std::cout << "Sequential result is: "<<seqresult<<std::endl;
	std::cout << "Loop cilkified result is: "<<parresult<<std::endl;
	std::cout << "Result is " << (close(seqresult,parresult,n)  ? "correct":"incorrect") << std::endl; 
	
	/**************************************************************/
	/********  START TESTING HYPEROBJECT CILKIFIED VERSION  *******/
	/**************************************************************/

	parresult = hyperobject_cilkified(a, b, n);   
	
	t1 = example_get_time();
	for(int i=0; i< ITERS; ++i)
	{
		parresult = hyperobject_cilkified(a, b, n);   
	}
 	t2 = example_get_time();

	partime = (t2-t1)/(ITERS*1000.f);
	std::cout << "Hyperobject cilkified time:" << partime << " seconds" << std::endl;
	std::cout << "Speedup is: " << seqtime/partime << std::endl;
	std::cout << "Sequential result is: "<<seqresult<<std::endl;
	std::cout << "Hyperobject result is: "<<parresult<<std::endl;
	std::cout << "Result is " << (close(seqresult,parresult,n)  ? "correct":"incorrect") << std::endl; 
    	
        
	delete [] a;
	delete [] b;
    	return 0;
}