예제 #1
0
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    std::vector<int> this_vec = {0,1,2,3,4,5,6};
    print_vec(this_vec);

    reverse_vector(this_vec);
    print_vec(this_vec);
    std::vector<int> big_vector = fill_vec(47);
    print_vec(big_vector);
    reverse_vector(big_vector);
    print_vec(big_vector);
    std::list<int> small_list = fill_list(15, 47);
    print_list(small_list);
    reverse_list(small_list);
    print_list(small_list);
    std::list<int> big_list = fill_list(100, 710);
    print_list(big_list);
    reverse_list(big_list);
    print_list(big_list);
    
    Node* a = new Node;
    a->value = 6;
    a->ptr = new Node;
    a->ptr->value = 7;
    a->ptr->ptr = new Node;
    a->ptr->ptr->value = 8;
    a->ptr->ptr->ptr = new Node;
    a->ptr->ptr->ptr->value = 9;
    a->ptr->ptr->ptr->ptr = NULL;
    // print out this list
    print_linked_list("a",a);
    
    // create an STL list with 4 elements
    std::list<int> b;
    b.push_back(10);
    b.push_back(11);
    b.push_back(12);
    b.push_back(13);
    
    // use the STL list as input to a creator function that creates
    // linked lists with the same data
    Node* c = make_linked_list_from_STL_list(b);
    // print that data
    print_linked_list("c",c);
    
    //
    // WRITE A FEW MORE TEST CASES OF make_linked_list_from_STL_list
    //
    
    
    // reverse a linked list of nodes
    Node* d = reverse_nodes_in_linked_list(c);
    // print this data
    print_linked_list("d",d);
    
    //
    // WRITE A FEW MORE TEST CASES OF reverse_nodes_in_linked_list
    return 0;
}
예제 #2
0
파일: gc.c 프로젝트: 9thScientist/SO
int global_clean() {

	char aux_path[PATH_SIZE], aux[CHUNK_SIZE];
	vec_str_t files;
	int i;

	strncpy(aux_path, getenv("HOME"), PATH_SIZE);
	strncat(aux_path, DATA_PATH, PATH_SIZE);

	vec_init(&files);
	fill_vec(aux_path, aux, CHUNK_SIZE, &files);

	for (i = 0; i < files.length; i++) {

		if (!is_linked(files.data[i]))  
			unlink(files.data[i]);
	}

	return 0;
}
예제 #3
0
int main(int argc, char ** argv)
{
  int      my_ID;           /* Thread ID                                         */
  int      vector_length;   /* length of vector loop containing the branch       */
  int      nfunc;           /* number of functions used in INS_HEAVY option      */
  int      rank;            /* matrix rank used in INS_HEAVY option              */
  double   branch_time,     /* timing parameters                                 */
           no_branch_time;
  double   ops;             /* double precision representation of integer ops    */
  int      iterations;      /* number of times the branch loop is carried out    */
  int      i, iter, aux;    /* dummies                                           */
  char     *branch_type;    /* string defining branching type                    */
  int      btype;           /* integer encoding branching type                   */
  int      total=0, 
           total_ref;       /* computed and stored verification values           */
  int      nthread_input;   /* thread parameters                                 */
  int      nthread; 
  int      num_error=0;     /* flag that signals that requested and obtained
                               numbers of threads are the same                  */

/**********************************************************************************
** process and test input parameters    
**********************************************************************************/

  if (argc != 5){
    printf("Usage:     %s <# threads> <# iterations> <vector length>", *argv);
    printf("<branching type>\n");
    printf("branching type: vector_go, vector_stop, no_vector, ins_heavy\n");
    exit(EXIT_FAILURE);
  }

  nthread_input = atoi(*++argv);
  if ((nthread_input < 1) || (nthread_input > MAX_THREADS)) {
    printf("ERROR: Invalid number of threads: %d\n", nthread_input);
    exit(EXIT_FAILURE);
  }

  omp_set_num_threads(nthread_input);

  iterations = atoi(*++argv);
  if (iterations < 1 || iterations%2==1){
     printf("ERROR: Iterations must be positive and even : %d \n", iterations);
     exit(EXIT_FAILURE);
  }

  vector_length  = atoi(*++argv);
  if (vector_length < 1){
     printf("ERROR: loop length must be >= 1 : %d \n",vector_length);
     exit(EXIT_FAILURE);
  }

  branch_type = *++argv;
  if      (!strcmp(branch_type,"vector_stop")) btype = VECTOR_STOP;
  else if (!strcmp(branch_type,"vector_go"  )) btype = VECTOR_GO;
  else if (!strcmp(branch_type,"no_vector"  )) btype = NO_VECTOR;
  else if (!strcmp(branch_type,"ins_heavy"  )) btype = INS_HEAVY;
  else  {
    printf("Wrong branch type: %s; choose vector_stop, vector_go, ", branch_type);
    printf("no_vector, or ins_heavy\n");
    exit(EXIT_FAILURE);
  }

  #pragma omp parallel private(i, my_ID, iter, aux, nfunc, rank) reduction(+:total)
  {
  int * RESTRICT vector; int * RESTRICT index;
  int factor = -1;

  #pragma omp master
  {
  nthread = omp_get_num_threads();

  printf("Parallel Research Kernels version %s\n", PRKVERSION);
  printf("OpenMP Branching Bonanza\n");
  if (nthread != nthread_input) {
    num_error = 1;
    printf("ERROR: number of requested threads %d does not equal ",
           nthread_input);
    printf("number of spawned threads %d\n", nthread);
  } 
  else {
    printf("Number of threads          = %d\n", nthread_input);
    printf("Vector length              = %d\n", vector_length);
    printf("Number of iterations       = %d\n", iterations);
    printf("Branching type             = %s\n", branch_type);
  }
  }
  bail_out(num_error);

  my_ID = omp_get_thread_num();

  vector = malloc(vector_length*2*sizeof(int));
  if (!vector) {
    printf("ERROR: Thread %d failed to allocate space for vector\n", my_ID);
    num_error = 1;
  }

  bail_out(num_error);

  /* grab the second half of vector to store index array                         */
  index   = vector + vector_length;

  /* initialize the array with entries with varying signs; array "index" is only 
     used to obfuscate the compiler (i.e. it won't vectorize a loop containing
     indirect referencing). It functions as the identity operator.               */
  for (i=0; i<vector_length; i++) { 
    vector[i]  = 3 - (i&7);
    index[i]   = i;
  }

  #pragma omp barrier   
  #pragma omp master
  {   
  branch_time = wtime();
  }

  /* do actual branching */

  switch (btype) {

    case VECTOR_STOP:
      /* condition vector[index[i]]>0 inhibits vectorization                     */
      for (iter=0; iter<iterations; iter+=2) {
        #pragma vector always
        for (i=0; i<vector_length; i++) { 
          aux = -(3 - (i&7));
          if (vector[index[i]]>0) vector[i] -= 2*vector[i];
          else                    vector[i] -= 2*aux;
        }
        #pragma vector always
        for (i=0; i<vector_length; i++) { 
          aux = (3 - (i&7));
          if (vector[index[i]]>0) vector[i] -= 2*vector[i];
          else                    vector[i] -= 2*aux;
        }
      }
      break;

    case VECTOR_GO:
      /* condition aux>0 allows vectorization                                    */
      for (iter=0; iter<iterations; iter+=2) {
        #pragma vector always
        for (i=0; i<vector_length; i++) {
          aux = -(3 - (i&7));
          if (aux>0) vector[i] -= 2*vector[i];
          else       vector[i] -= 2*aux;
        }
        #pragma vector always
        for (i=0; i<vector_length; i++) {
          aux = (3 - (i&7));
          if (aux>0) vector[i] -= 2*vector[i];
          else       vector[i] -= 2*aux;
        }
      }
      break;

    case NO_VECTOR:
      /* condition aux>0 allows vectorization, but indirect indexing inbibits it */
      for (iter=0; iter<iterations; iter+=2) {
        #pragma vector always
        for (i=0; i<vector_length; i++) {
          aux = -(3 - (i&7));
          if (aux>0) vector[i] -= 2*vector[index[i]];
          else       vector[i] -= 2*aux;
        }
        #pragma vector always
        for (i=0; i<vector_length; i++) {
          aux = (3 - (i&7));
          if (aux>0) vector[i] -= 2*vector[index[i]];
          else       vector[i] -= 2*aux;
        }
      }
      break;

    case INS_HEAVY:
      fill_vec(vector, vector_length, iterations, WITH_BRANCHES, &nfunc, &rank);
    }
    #pragma omp master
    {
    branch_time = wtime() - branch_time;
    if (btype == INS_HEAVY) {
      printf("Number of matrix functions = %d\n", nfunc);
      printf("Matrix order               = %d\n", rank);
    }
    }

    /* do the whole thing once more, but now without branches                    */

    #pragma omp barrier
    #pragma omp master
    {   
    no_branch_time = wtime();
    }

    /* do actual branching */

    switch (btype) {

    case VECTOR_STOP:
    case VECTOR_GO:
      for (iter=0; iter<iterations; iter+=2) {
        #pragma vector always
        for (i=0; i<vector_length; i++) { 
          aux = -(3-(i&7)); 
          vector[i] -= (vector[i] + aux);
        }
        for (i=0; i<vector_length; i++) {
          aux = (3-(i&7)); 
          vector[i] -= (vector[i] + aux);
        }
      }
      break;

    case NO_VECTOR:
      for (iter=0; iter<iterations; iter+=2) {
        #pragma vector always
        for (i=0; i<vector_length; i++) {
          aux = -(3-(i&7));
          vector[i] -= (vector[index[i]]+aux); 
        }
        #pragma vector always
        for (i=0; i<vector_length; i++) {
          aux = (3-(i&7));
          vector[i] -= (vector[index[i]]+aux); 
        }
      }
      break;

    case INS_HEAVY:
      fill_vec(vector, vector_length, iterations, WITHOUT_BRANCHES, &nfunc, &rank);
    }

    #pragma omp master
    {
    no_branch_time = wtime() - no_branch_time;
    ops = (double)vector_length * (double)iterations * (double)nthread;
    if (btype == INS_HEAVY) ops *= rank*(rank*19 + 6);
    else                    ops *= 4;
    }

    for (total = 0, i=0; i<vector_length; i++) total += vector[i];
  } /* end of OPENMP parallel region                                             */

  /* compute verification values                                                 */
  total_ref = ((vector_length%8)*(vector_length%8-8) + vector_length)/2*nthread;

  if (total == total_ref) {
    printf("Solution validates\n");
    printf("Rate (Mops/s) with branches:    %lf time (s): %lf\n", 
           ops/(branch_time*1.e6), branch_time);
    printf("Rate (Mops/s) without branches: %lf time (s): %lf\n", 
           ops/(no_branch_time*1.e6), no_branch_time);
#ifdef VERBOSE
    printf("Array sum = %d, reference value = %d\n", total, total_ref);
#endif     
  }
  else {
    printf("ERROR: array sum = %d, reference value = %d\n", total, total_ref);
  }

  exit(EXIT_SUCCESS);
}
예제 #4
0
파일: branch.c 프로젝트: afanfa/Kernels
int main(int argc, char ** argv)
{
  int        my_ID;           /* rank                                            */
  int        root=0;          /* ID of root rank                                 */
  int        Num_procs;       /* Number of ranks                                 */
  int        vector_length;   /* length of loop containing the branch            */
  int        nfunc;           /* number of functions used in INS_HEAVY option    */
  int        rank;            /* matrix rank used in INS_HEAVY option            */
  double     branch_time,     /* timing parameters                               */
             no_branch_time;
  double     ops;             /* number of integer operations in code            */
  int        iterations;      /* number of times the branching loop is executed  */
  int        i, iter, aux;    /* dummies                                         */
  int        error = 0;       /* error flag                                      */
  char       *branch_type;
  int        total=0, 
             total_sum,
             total_ref;       /* computed and stored verification values         */
  int        btype;
  int        * RESTRICT vector, * RESTRICT index;

/*********************************************************************
** Initialize the MPI environment
*********************************************************************/
  MPI_Init(&argc,&argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &my_ID);
  MPI_Comm_size(MPI_COMM_WORLD, &Num_procs);

/**********************************************************************************
** process and test input parameters    
***********************************************************************************/

  if(my_ID == root) {
    printf("Parallel Research Kernels version %s\n", PRKVERSION);
    printf("MPI Branching Bonanza\n");

    if (argc != 4){
      printf("USAGE:     %s <# iterations> <loop length> <branching type>\n", *argv);
      printf("branching type: vector_go, vector_stop, no_vector, ins_heavy\n");
      error = 1;
      goto ENDOFTESTS;
    }

    iterations = atoi(*++argv);
    if (iterations < 1 || iterations%2==1){
       printf("ERROR: Iterations must be positive and even : %d \n", iterations);
       error = 1;
       goto ENDOFTESTS;
    }

    vector_length  = atoi(*++argv);
    if (vector_length < 1){
       printf("ERROR: loop length must be >= 1 : %d \n",vector_length);
       error = 1;
       goto ENDOFTESTS;
    }

    branch_type = *++argv;
    if      (!strcmp(branch_type,"vector_stop")) btype = VECTOR_STOP;
    else if (!strcmp(branch_type,"vector_go"  )) btype = VECTOR_GO;
    else if (!strcmp(branch_type,"no_vector"  )) btype = NO_VECTOR;
    else if (!strcmp(branch_type,"ins_heavy"  )) btype = INS_HEAVY;
    else  {
      printf("Wrong branch type: %s; choose vector_stop, vector_go, ", branch_type);
      printf("no_vector, or ins_heavy\n");
      error = 1;
      goto ENDOFTESTS;
    }
    ENDOFTESTS:;
  }
  bail_out(error);

  if (my_ID == root) {
    printf("Number of ranks            = %d\n", Num_procs);
    printf("Vector length              = %d\n", vector_length);
    printf("Number of iterations       = %d\n", iterations);
    printf("Branching type             = %s\n", branch_type);
  }

  /* broadcast input data  */
  MPI_Bcast(&vector_length, 1, MPI_INT, root, MPI_COMM_WORLD);
  MPI_Bcast(&iterations,    1, MPI_INT, root, MPI_COMM_WORLD);
  MPI_Bcast(&btype,         1, MPI_INT, root, MPI_COMM_WORLD);

  vector = prk_malloc(vector_length*2*sizeof(int));
  if (!vector) {
    printf("ERROR: rank %d failed to allocate space for vector\n", my_ID);
    error = 1;
  }
  bail_out(error);

  index   = vector + vector_length;

  /* initialize the array with entries with varying signs              */
  for (i=0; i<vector_length; i++) { 
    vector[i]  = 3 - (i&7);
    index[i]   = i;
  }

  MPI_Barrier(MPI_COMM_WORLD);   
  branch_time = wtime();

  /* do actual branching */

  switch (btype) {

    case VECTOR_STOP:
      for (iter=0; iter<iterations; iter+=2) {
        #pragma vector always
        for (i=0; i<vector_length; i++) { 
          aux = -(3 - (i&7));
          if (vector[index[i]]>0) vector[i] -= 2*vector[i];
          else                    vector[i] -= 2*aux;
        }
        #pragma vector always
        for (i=0; i<vector_length; i++) { 
          aux = (3 - (i&7));
          if (vector[index[i]]>0) vector[i] -= 2*vector[i];
          else                    vector[i] -= 2*aux;
        }
      }
      break;

    case VECTOR_GO:
      for (iter=0; iter<iterations; iter+=2) {
        #pragma vector always
        for (i=0; i<vector_length; i++) {
          aux = -(3 - (i&7));
          if (aux>0) vector[i] -= 2*vector[i];
          else       vector[i] -= 2*aux;
        }
        #pragma vector always
        for (i=0; i<vector_length; i++) {
          aux = (3 - (i&7));
          if (aux>0) vector[i] -= 2*vector[i];
          else       vector[i] -= 2*aux;
        }
      }
      break;

    case NO_VECTOR:
      for (iter=0; iter<iterations; iter+=2) {
        #pragma vector always
        for (i=0; i<vector_length; i++) {
          aux = -(3 - (i&7));
          if (aux>0) vector[i] -= 2*vector[index[i]];
          else       vector[i] -= 2*aux;
        }
        #pragma vector always
        for (i=0; i<vector_length; i++) {
          aux = (3 - (i&7));
          if (aux>0) vector[i] -= 2*vector[index[i]];
          else       vector[i] -= 2*aux;
        }
      }
      break;

    case INS_HEAVY:
      fill_vec(vector, vector_length, iterations, WITH_BRANCHES, &nfunc, &rank);
  }

  branch_time = wtime() - branch_time;

  if (btype == INS_HEAVY && my_ID==root) {
    printf("Number of matrix functions = %d\n", nfunc);
    printf("Matrix order               = %d\n", rank);
  }

  /* do the whole thing once more, but now without branches              */

  MPI_Barrier(MPI_COMM_WORLD);
  no_branch_time = wtime();

  /* do actual branching */

  switch (btype) {

  case VECTOR_STOP:
  case VECTOR_GO:
    for (iter=0; iter<iterations; iter+=2) {
      #pragma vector always
      for (i=0; i<vector_length; i++) { 
        aux = -(3-(i&7)); 
        vector[i] -= (vector[i] + aux);
      }
      for (i=0; i<vector_length; i++) {
        aux = (3-(i&7)); 
        vector[i] -= (vector[i] + aux);
      }
    }
    break;

  case NO_VECTOR:
    for (iter=0; iter<iterations; iter+=2) {
      #pragma vector always
      for (i=0; i<vector_length; i++) {
        aux = -(3-(i&7));
        vector[i] -= (vector[index[i]]+aux); 
      }
      #pragma vector always
      for (i=0; i<vector_length; i++) {
        aux = (3-(i&7));
        vector[i] -= (vector[index[i]]+aux); 
      }
    }
    break;

  case INS_HEAVY:
    fill_vec(vector, vector_length, iterations, WITHOUT_BRANCHES, &nfunc, &rank);
  }

  no_branch_time = wtime() - no_branch_time;

  total = 0;
  for (i=0; i<vector_length; i++) total += vector[i];

  MPI_Reduce(&total, &total_sum, 1, MPI_INT, MPI_SUM, root, MPI_COMM_WORLD);

  /* compute verification values                                             */
  total_ref = ((vector_length%8)*(vector_length%8-8) + vector_length)/2*Num_procs;

  if (my_ID == root) {
    ops = (double)vector_length * (double)iterations * (double)Num_procs;
    if (btype == INS_HEAVY) ops *= rank*(rank*19 + 6);
    else                    ops *= 4;
    if (total_sum == total_ref) {
      printf("Solution validates\n");
      printf("Rate (Mops/s) with branches:    %lf time (s): %lf\n", 
             ops/(branch_time*1.e6), branch_time);
      printf("Rate (Mops/s) without branches: %lf time (s): %lf\n", 
             ops/(no_branch_time*1.e6), no_branch_time);
#if VERBOSE
      printf("Array sum = %d, reference value = %d\n", total_sum, total_ref);
#endif     
    }
    else {
      printf("ERROR: array sum = %d, reference value = %d\n", total, total_ref);
    }
  }

  MPI_Finalize();
  exit(EXIT_SUCCESS);
}