void read_inputs() { int i,j,k,temp; char c; c=getchar(); //scanf("%d",&T_num); T_num=chartoi(c); c=getchar(); s=malloc(10000*sizeof(short int)); for(i =0; i<T_num; i++) { j=0; c=getchar(); do { s[j]=chartoi(c); c=getchar(); j++; } while(c!='\n'); size=j; first=second=third=fourth=-1; check(); } }
void RTF_backslash(FILE* f, char** pch, char* pf) { int ch; *pf=FALSE; ch = RTF_GetChar( f ); if(feof(f)) { fprintf(stderr,"Unexpected end of file\n"); return; } switch (ch) { case '\\': *pch=charset_table[92]; *pf=TRUE; break; case '{': *pch=charset_table[123]; *pf=TRUE; break; case '}': *pch=charset_table[125]; *pf=TRUE; break; case '*': gobble = TRUE; /*perform no output, ignore commands 'til level-1*/ if(skip_to_level>level-1||skip_to_level==-1) skip_to_level = level-1; break; case '\'': { char ch1, ch2; ch1 = RTF_GetChar( f ); ch2 = RTF_GetChar( f ); if(!feof(f)) { if(isxdigit(ch1)&&isxdigit(ch2)) { ch = chartoi(ch1)*16+chartoi(ch2); *pch = charset_table[ch-1]; *pf=TRUE; } else { fprintf(stderr,"RTF Error: unexpected '%c%c' after \\\'\n",ch1,ch2); } } break; } default: if (isalpha(ch)) { RTF_BuildToken(f, ch); } else { fprintf(stderr, "\nRTF Error: unexpected '%c' after \\.\n", ch); } break; } }
int main(int argc, char ** argv) { int my_ID; /* rank */ int root=0; int iterations; /* number of times to rehash strings */ int i, iter; /* dummies */ int checksum; /* computed checksum of final aggregate string */ char *scramble = "27638472638746283742712311207892"; char *basestring; /* initial string to be copied to private strings */ char *iterstring; /* private copy of string */ char *catstring; /* concatenated, scrambled string */ long length, /* total length of scramble string */ proc_length; /* length of string per rank */ int basesum; /* checksum of base string */ MPI_Datatype mpi_word; /* chunk of scramble string to be communicated */ double stopngo_time;/* timing parameter */ int Num_procs; /* Number of ranks */ int error = 0; /* error flag */ /********************************************************************************* ** 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, test and broadcast input parameter **********************************************************************************/ if (my_ID == root){ printf("Parallel Research Kernels version %s\n", PRKVERSION); printf("MPI global synchronization\n"); if (argc != 3){ printf("Usage: %s <# iterations> <scramble string length>\n", *argv); error = 1; goto ENDOFTESTS; } iterations = atoi(*++argv); if (iterations < 1){ printf("ERROR: iterations must be positive: %d \n",iterations); error = 1; goto ENDOFTESTS; } length = atol(*++argv); if (length <Num_procs || length%Num_procs !=0) { printf("ERROR: length of string %ld must be multiple of # ranks: %d\n", length, Num_procs); error = 1; goto ENDOFTESTS; } ENDOFTESTS:; } bail_out(error); if (my_ID == root) { printf("Number of ranks = %d\n", Num_procs); printf("Scramble string length = %ld\n", length); printf("Number of iterations = %d\n", iterations); } /* Broadcast benchmark data to all ranks */ MPI_Bcast(&iterations, 1, MPI_INT, root, MPI_COMM_WORLD); MPI_Bcast(&length, 1, MPI_LONG, root, MPI_COMM_WORLD); proc_length = length/Num_procs; basestring = malloc((proc_length+1)*sizeof(char)); if (basestring==NULL) { printf("ERROR: Could not allocate space for scramble string\n"); error = 1; } bail_out(error); /* fill the base string with copies (truncated) of scrable string */ for (i=0; i<proc_length; i++) basestring[i]=scramble[i%32]; basestring[proc_length] = EOS; catstring=(char *) malloc((length+1)*sizeof(char)); if (catstring==NULL) { printf("ERROR: Could not allocate space for concatenation string: %ld\n", length+1); error = 1; } bail_out(error); /* initialize concatenation string with nonsense */ for (i=0; i<length; i++) catstring[i]='9'; catstring[length]=EOS; iterstring= (char *) malloc((proc_length+1)*sizeof(char)); if (iterstring==NULL) { printf("ERROR: Could not allocate space for strings in rank %d\n", my_ID); error = 1; } bail_out(error); strcpy(iterstring, basestring); catstring=(char *) malloc((length+1)*sizeof(char)); if (catstring==NULL) { printf("ERROR: Could not allocate space for strings in rank %d\n", my_ID); error = 1; } bail_out(error); for (i=0; i<length; i++) catstring[i]='9'; catstring[length]=EOS; /* define a user type consisting of a chunk of chars to be communicated */ MPI_Type_contiguous(proc_length,MPI_CHAR, &mpi_word); MPI_Type_commit(&mpi_word); MPI_Barrier(MPI_COMM_WORLD); stopngo_time = wtime(); for (iter=0; iter<iterations; iter++) { /* Everybody sends own string to everybody else and concatenates */ MPI_Allgather(iterstring,1,mpi_word, catstring,1,mpi_word, MPI_COMM_WORLD); /* now everybody selects a different substring */ for (i=0; i<proc_length; i++) iterstring[i]=catstring[my_ID+i*Num_procs]; #ifdef VERBOSE if (my_ID==0) { checksum=0; for (i=0; i<length+1;i++) checksum+= chartoi(catstring[i]); printf("Iteration %d, cat string %s, checksum equals: %d\n", iter, catstring, checksum); } #endif } stopngo_time = wtime() - stopngo_time; /* compute checksum on obtained result, adding all digits in the string */ if (my_ID==0) { basesum=0; for (i=0; i<proc_length; i++) basesum += chartoi(basestring[i]); checksum=0; for (i=0; i<length+1;i++) checksum += chartoi(catstring[i]); if (checksum != basesum*Num_procs) { printf("Incorrect checksum: %d instead of %d\n", checksum, basesum*Num_procs); } else { printf("Solution validates\n"); } printf("Rate (synch/s): %lf, time (s): %lf\n", (iterations/stopngo_time), stopngo_time); } MPI_Finalize(); } /* end of main */
int main(int argc, char ** argv) { int my_ID; /* Thread ID */ int iterations; /* number of times to rehash strings */ int i, iter; /* dummy */ int checksum; /* computed checksum of final aggregate string */ char *scramble = "27638472638746283742712311207892"; char *basestring; /* initial string to be copied to private strings */ char *iterstring; /* private copy of string */ char *catstring; /* concatenated, scrambled string */ long length; /* length of scramble string */ long thread_length; /* string length per thread */ int basesum; /* checksum of base string */ double stopngo_time; /* timing parameter */ int nthread_input, /* thread parameters */ nthread; int num_error=0; /* flag that signals that requested and obtained numbers of threads are the same */ /************************************************************************** ** process, and test input parameter ***************************************************************************/ if (argc != 4){ printf("Usage: %s <# threads> <# iterations> <scramble string length>\n", *argv); exit(EXIT_FAILURE); } /* Take number of threads to request from command line */ 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){ printf("ERROR: iterations must be >= 1 : %d \n",iterations); exit(EXIT_FAILURE); } length = atol(*++argv); if (length <nthread_input || length%nthread_input !=0) { printf("ERROR: length of string %d must be multiple of # threads: %d\n", length, nthread_input); exit(EXIT_FAILURE); } thread_length = length/nthread_input; basestring = malloc((thread_length+1)*sizeof(char)); if (basestring==NULL) { printf("ERROR: Could not allocate space for scramble string\n"); exit(EXIT_FAILURE); } /* fill the base string with copies (truncated) of scrable string */ for (i=0; i<thread_length; i++) basestring[i]=scramble[i%32]; basestring[thread_length] = EOS; catstring=(char *) malloc((length+1)*sizeof(char)); if (catstring==NULL) { printf("ERROR: Could not allocate space for concatenation string: %d\n", length+1); exit(EXIT_FAILURE); } /* initialize concatenation string with nonsense */ for (i=0; i<length; i++) catstring[i]='9'; catstring[length]=EOS; #pragma omp parallel private(iterstring, my_ID, i, iter) { my_ID = omp_get_thread_num(); /* everybody receives a private copy of the base string */ iterstring = (char *) malloc((thread_length+1)*sizeof(char)); if (!iterstring) { printf("ERROR: Thread %d could not allocate space for private string\n", my_ID); num_error = 1; } bail_out(num_error); strcpy(iterstring, basestring); #pragma omp master { nthread = omp_get_num_threads(); printf("OpenMP global synchronization test\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("Length of scramble string = %ld\n", length); printf("Number of iterations = %d\n", iterations); } } bail_out(num_error); #pragma omp master { stopngo_time = wtime(); } for (iter=0; iter<iterations; iter++) { /* we need a barrier to avoid reading catstring before it is complete */ #pragma omp barrier /* glue all private strings together */ strncpy(catstring+my_ID*thread_length,iterstring,(size_t) thread_length); /* synchronize so we can read the consistent concatenated string */ #pragma omp barrier /* now all threads select different, nonoverlapping substring */ for (i=0; i<thread_length; i++) iterstring[i]=catstring[my_ID+i*nthread]; #ifdef VERBOSE #pragma omp master { checksum=0; for (i=0; i<strlen(catstring);i++) checksum+= chartoi(catstring[i]); printf("Iteration %d, cat string %s, checksum equals: %d\n", iter, catstring, checksum); } #endif } #pragma omp master { stopngo_time = wtime() - stopngo_time; } } /* end of parallel region */ /* compute checksum on obtained result, adding all digits in the string */ basesum=0; for (i=0; i<thread_length; i++) basesum += chartoi(basestring[i]); checksum=0; for (i=0; i<strlen(catstring);i++) checksum += chartoi(catstring[i]); if (checksum != basesum*nthread) { printf("Incorrect checksum: %d instead of %d\n", checksum, basesum*nthread); exit(EXIT_FAILURE); } else { #ifdef VERBOSE printf("Solution validates; Correct checksum of %d\n", checksum); #else printf("Solution validates\n"); #endif } printf("Rate (synch/s): %e, time (s): %lf\n", (((double)iterations)/stopngo_time), stopngo_time); exit(EXIT_SUCCESS); } /* end of main */