void start(){ masses[0] = EARTH_MASS*500000.0; pos[0][X]=0*EARTH_SUN; pos[0][Y]=0*EARTH_SUN; vel[0][X]=0*EARTH_SUN; vel[0][Y]=0*EARTH_SUN; newPos[0][X]=0*EARTH_SUN; newPos[0][Y]=0*EARTH_SUN; newVel[0][X]=0*EARTH_SUN; newVel[0][Y]=0*EARTH_SUN; for(int i=1;i<N;i++){ double distance = 0; masses[i] = EARTH_MASS*Ranf( 0.999f, 1.0f ); do { pos[i][X]=EARTH_SUN*Ranf( -500.0f, 500.f ); pos[i][Y]=EARTH_SUN*Ranf( -500.0f, 500.f ); distance = sqrt(pos[i][Y]*pos[i][Y]+pos[i][X]*pos[i][X]); }while(distance<EARTH_SUN*100 ); vel[i][X]=pos[i][Y]/(distance*sqrt(distance))*SPEED_FACTOR; vel[i][Y]=-pos[i][X]/(distance*sqrt(distance))*SPEED_FACTOR; newPos[i][X]=pos[i][X]; newPos[i][Y]=pos[i][Y]; newVel[i][X]=vel[i][X]; newVel[i][Y]=vel[i][Y]; } printf("Procesando...\n"); double time0 = omp_get_wtime(); solve(T); double time1 = omp_get_wtime(); /**Impresion del tiempo que tardo el algoritmo para determinar las * posiciones y velocidades de las particulas ***/ printf("duracion: %f\n",time1-time0); }
void start(){ best_tour= malloc(sizeof(tour)); stack= malloc(sizeof(Stack)); for(int i=0;i<N;i++){ digraph[i][i] = 0; point[i][0] = Ranf( -G, G ); point[i][1] = Ranf( -G, G ); best_tour->path[i] = 0; t1.path[i] = 0; } for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ digraph[i][j] = sqrt((point[i][0]-point[j][0])*(point[i][0]-point[j][0])+(point[i][1]-point[j][1])*(point[i][1]-point[j][1])); } } best_tour->cost = RAND_MAX; best_tour->last = 0; best_tour->first = 0; best_tour->n = 1; t1.cost = 0; t1.last = 0; t1.first = 0; t1.n = 1; stack->max = N*((N-1)*0.5); stack->top = -1; stack->content = malloc((stack->max)*sizeof(int)); }
int Wolf(int i) { // affected by NowDeer while( NowYear <= 2020 ) { // compute a temporary next-value for this quantity based on the current state of the simulation: int tmpNumWolf = NowNumWolf; if (NowNumDeer > 3 * tmpNumWolf || NowNumDeer < tmpNumWolf) tmpNumWolf-= (int) (2.0*Ranf(0.2,0.9)); else tmpNumWolf+= (int) (2.0*Ranf(0.3,0.9)); if (tmpNumWolf < 0) tmpNumWolf = 0 ; // DoneComputing barrier: #pragma omp barrier { NowNumWolf = tmpNumWolf; } // DoneAssigning barrier: #pragma omp barrier { // do nothing } // DonePrinting barrier: #pragma omp barrier } return i; }
int main(int argc, char *argv[ ]) { double prec = omp_get_wtick(); //fprintf( stderr, "Clock precision = %g\n", prec ); for (int i = 0; i < NUM; i++) { A[i] = Ranf(-10.f, 10.f); B[i] = Ranf(-10.f, 10.f); } /**************************** * SIMD test block * **************************/ double time0 = Timer(); for (int t = 0; t < NUM_TRIALS; t++) { SimdMul(A, B, C, NUM); } double time1 = Timer(); double dts = (time1 - time0) / (float) NUM_TRIALS; if (PRINT_SIMD == 1) { if(GNUPLOT == 0) { printf("Average SIMD Elapsed time = %g\n", dts); printf("SIMD speed = %8.3f MFLOPS\n", ((float) NUM / dts) / 1000000.f); } else { // x-axis: #-of-elements y-axis: MFLOPS, do not need elapsed time printf("%d %8.3f\n", NUM, ((float) NUM / dts) / 1000000.f); } } /**************************** * non SIMD test block * **************************/ double time2 = Timer(); for (int t = 0; t < NUM_TRIALS; t++) { NonSimdMul(A, B, C, NUM); } double time3 = Timer(); double dtn = (time3 - time2) / (float) NUM_TRIALS; if(PRINT_NOSIMD == 1) { if(GNUPLOT == 0) { printf("Average Non-SIMD Elapsed time = %g\n", dtn); printf("Non-SIMD speed = %8.3f MFLOPS\n", ((float) NUM / dtn) / 1000000.f); //printf("Speed-up = %g\n", dtn / dts); } else { // x-axis: #-of-elements y-axis: MFLOPS, do not need elapsed time printf("%d %8.3f\n", NUM, ((float) NUM / dtn) / 1000000.f); } } if(PRINT_DIFFERENCE == 1) { printf("%d %g\n", NUM, ((float) NUM / dtn) / (dtn/dts)); } return 0; }
int main(int argc, char *argv[ ]) { #ifndef _OPENMP fprintf( stderr, "OpenMP is not available\n" ); return 1; #endif // set up global variables // starting date and time: NowMonth = 0; NowYear = 2015; // starting state NowNumDeer = 1; NowHeight = 1.; NowNumWolf = 1; // computing temp and precip float ang = ( 30.*(float)NowMonth + 15. ) * ( M_PI / 180. ); // float temp = AVG_TEMP - AMP_TEMP * cos( ang ); NowTemp = temp + Ranf( -RANDOM_TEMP, RANDOM_TEMP ); // float precip = AVG_PRECIP_PER_MONTH + AMP_PRECIP_PER_MONTH * sin( ang ); NowPrecip = precip + Ranf( -RANDOM_PRECIP, RANDOM_PRECIP ); if( NowPrecip < 0. ) NowPrecip = 0.; // spawn # threads omp_set_num_threads( 4 ); #pragma omp parallel sections // parallel section directive // functional decomposition { #pragma omp section { GrainDeer(1); } #pragma omp section { Wolf(1); } #pragma omp section { GrainGrow(1); } #pragma omp section { Watcher(1); } } // implied barrier -- all functions must return to get past here return 0; }
void ResetParticles( ) { glBindBuffer( GL_ARRAY_BUFFER, hPobj ); struct xyzw *points = (struct xyzw *) glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY ); for( int i = 0; i < NUM_PARTICLES; i++ ) { points[i].x = Ranf( XMIN, XMAX ); points[i].y = Ranf( YMIN, YMAX ); points[i].z = Ranf( ZMIN, ZMAX ); points[i].w = 1.; } glUnmapBuffer( GL_ARRAY_BUFFER ); glBindBuffer( GL_ARRAY_BUFFER, hCobj ); struct rgba *colors = (struct rgba *) glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY ); for( int i = 0; i < NUM_PARTICLES; i++ ) { colors[i].r = Ranf( .3f, 1. ); colors[i].g = Ranf( .3f, 1. ); colors[i].b = Ranf( .3f, 1. ); colors[i].a = 1.; } glUnmapBuffer( GL_ARRAY_BUFFER ); for( int i = 0; i < NUM_PARTICLES; i++ ) { hVel[i].x = Ranf( VMIN, VMAX ); hVel[i].y = Ranf( 0., VMAX ); hVel[i].z = Ranf( VMIN, VMAX ); } }
void calcTempAndPrecip(void *t){ //Must atomize so we don't have race conditions between months. pthread_mutex_lock(&tempAndPrec); float ang = ( 30.*(float)NowMonth + 15. ) * ( M_PI / 180. ); float temp = AVG_TEMP - AMP_TEMP * cos( ang ); NowTemp = temp + Ranf( -RANDOM_TEMP, RANDOM_TEMP ); float precip = AVG_PRECIP_PER_MONTH + AMP_PRECIP_PER_MONTH * sin( ang ); NowPrecip = precip + Ranf( -RANDOM_PRECIP, RANDOM_PRECIP ); if( NowPrecip < 0. ){ NowPrecip = 0.; } pthread_mutex_unlock(&tempAndPrec); }
int Ranf( int ilow, int ihigh ) { float low = (float)ilow; float high = (float)ihigh + 0.9999f; return (int)( Ranf(low,high) ); }
int Watcher(int i) { while( NowYear <= 2020 ) { // compute a temporary next-value for this quantity based on the current state of the simulation: { // do nothing } // DoneComputing barrier: #pragma omp barrier { // do nothing } // DoneAssigning barrier: #pragma omp barrier { //print fprintf(stdout, "%d01\t%f\t%f\t%f\t%d\t%d\n", NowYear*100+(NowMonth+1), (5.0/9.0)*(NowTemp-32.0), NowPrecip*2.54, NowHeight*2.54, NowNumDeer, NowNumWolf); //increase month NowMonth++; NowMonth = NowMonth % 12; if (NowMonth == 0) { //increase year NowYear++; } // computing temp and precip float ang = ( 30.*(float)NowMonth + 15. ) * ( M_PI / 180. ); float temp = AVG_TEMP - AMP_TEMP * cos( ang ); NowTemp = temp + Ranf( -RANDOM_TEMP, RANDOM_TEMP ); float precip = AVG_PRECIP_PER_MONTH + AMP_PRECIP_PER_MONTH * sin( ang ); NowPrecip = precip + Ranf( -RANDOM_PRECIP, RANDOM_PRECIP ); if( NowPrecip < 0. ) NowPrecip = 0.; } // DonePrinting barrier: #pragma omp barrier } return i; }
// Initialize agents with random positions void init_data(sAgents &h_agents_in) { const float PI = 3.1415926535; const float XMIN = { 2.0 }; const float XMAX = { world_width }; const float YMIN = { 2.0 }; const float YMAX = { world_height }; const float THMIN = { 0.0}; const float THMAX = { 2*PI }; //2*PI const float VMIN = { 0.5 }; //0.0 const float VMAX = { 1.0 }; for(int i = 0; i< agents_total;i++) { h_agents_in.ids[i].x = i + 1; h_agents_in.ids[i].y = 0; h_agents_in.pos[i].x = round(Ranf( XMIN, XMAX )); h_agents_in.pos[i].y = round(Ranf( YMIN, YMAX )); h_agents_in.pos[i].z = Ranf( THMIN, THMAX ); h_agents_in.pos[i].w = Ranf( VMIN, VMAX ); } }
// Initialize agents with random positions void init_data(agent *h_agents_in, int agents_total) { const float PI = 3.1415926535; const float XMIN = { 1.0 }; const float XMAX = { 96.0 }; const float YMIN = { 1.0 }; const float YMAX = { 96.0 }; const float THMIN = { 0.0}; const float THMAX = { 2*PI }; //2*PI const float VMIN = { 0.0 }; //0.0 const float VMAX = { 1.0 }; for( int i = 0; i < agents_total; i++ ) { h_agents_in[ i ].id = i; h_agents_in[ i ].x = round(Ranf( XMIN, XMAX )); h_agents_in[ i ].y = round(Ranf( YMIN, YMAX )); h_agents_in[ i ].z = Ranf( THMIN, THMAX); h_agents_in[ i ].w = Ranf( VMIN, VMAX); //printf("i: %d x: %f y: %f z: %f, w: %f \n", i, h_dptr[i].x, h_dptr[i].y, h_dptr[i].z, h_dptr[i].w); } }
int main( int argc, char *argv[ ] ) { #ifndef _OPENMP fprintf( stderr, "OpenMP is not available\n" ); return 1; #endif omp_set_num_threads( NUMT ); int numProcessors = omp_get_num_procs( ); fprintf( stderr, "Have %d processors.\n", numProcessors ); for ( int i = 0; i < NUMBODIES; i++ ) { Bodies[i].mass = EARTH_MASS * Ranf( 0.5f, 10.f ); Bodies[i].x = EARTH_DIAMETER * Ranf( -100.f, 100.f ); Bodies[i].y = EARTH_DIAMETER * Ranf( -100.f, 100.f ); Bodies[i].z = EARTH_DIAMETER * Ranf( -100.f, 100.f ); Bodies[i].vx = Ranf( -100.f, 100.f );; Bodies[i].vy = Ranf( -100.f, 100.f );; Bodies[i].vz = Ranf( -100.f, 100.f );; }; double time0 = omp_get_wtime( ); for ( int t = 0; t < NUMSTEPS; t++ ) { // #pragma omp parallel for schedule(dynamic) for ( int i = 0; i < NUMBODIES; i++ ) { float fx = 0.; float fy = 0.; float fz = 0.; Body *bi = &Bodies[i]; #pragma omp parallel for reduction(+:fx,fy,fz) schedule(dynamic) for ( int j = 0; j < NUMBODIES; j++ ) { if ( j == i ) continue; Body *bj = &Bodies[j]; float rsqd = GetDistanceSquared( bi, bj ); if ( rsqd > 0. ) { float f = G * bi->mass * bj->mass / rsqd; float ux, uy, uz; GetUnitVector( bi, bj, &ux, &uy, &uz ); fx += f * ux; fy += f * uy; fz += f * uz; } } float ax = fx / Bodies[i].mass; float ay = fy / Bodies[i].mass; float az = fz / Bodies[i].mass; Bodies[i].xnew = Bodies[i].x + Bodies[i].vx * TIMESTEP + 0.5 * ax * TIMESTEP * TIMESTEP; Bodies[i].ynew = Bodies[i].y + Bodies[i].vy * TIMESTEP + 0.5 * ay * TIMESTEP * TIMESTEP; Bodies[i].znew = Bodies[i].z + Bodies[i].vz * TIMESTEP + 0.5 * az * TIMESTEP * TIMESTEP; Bodies[i].vxnew = Bodies[i].vx + ax * TIMESTEP; Bodies[i].vynew = Bodies[i].vy + ay * TIMESTEP; Bodies[i].vznew = Bodies[i].vz + az * TIMESTEP; } // setup the state for the next animation step: for ( int i = 0; i < NUMBODIES; i++ ) { Bodies[i].x = Bodies[i].xnew; Bodies[i].y = Bodies[i].ynew; Bodies[i].z = Bodies[i].znew; Bodies[i].vx = Bodies[i].vxnew; Bodies[i].vy = Bodies[i].vynew; Bodies[i].vz = Bodies[i].vznew; } } // t double time1 = omp_get_wtime( ); // print performance here::: double mbodies = (float)(NUMBODIES * NUMBODIES * NUMSTEPS) / (time1 - time0) / 1000000.; printf("Performance = %8.2lf MegaBodies/Sec\n", mbodies); printf("Time = %8.2lf MegaSecs\n", (time1 - time0) * 1000000.); return 0; }