Ejemplo n.º 1
0
/**
 * Lance la procédure de répartition de la chaleur sur une nouvelle matrice.
 * */
void lancer_programme() {
    init();
    printf("Etape 0 :\n");
    print_matrice(mat);
    for(int i = 0; i < NB_EXE; i++) {
        printf("Etape %d :\n", i + 1);
        diffuser_chaleur(current, TAILLE_MATRICE/2 + i);
    }  
}
Ejemplo n.º 2
0
void diffuser_chaleur(MAT m, int j) {
    /*float temp = m[i][j];
    //printf("i : %d, j : %d, temp : %f\n",i,j,(1/H));
    m[i][j-1] = temp*(1/H);
    m[i][j+1] = temp*(1/H);
    m[i][j] = temp*(4/H);*/

    // on propage la chaleur selon les x vers la droite
    for(int i = 0; i < TAILLE_MATRICE; i++) {
        m[i][j] = ((1/H) * mat[i][j - 1]) + ((4/H) * mat[i][j]) + ((1/H) * mat[i] [j + 1]);
        m[i][j - 1] = ((1/H) * mat[i][j - 1]) + ((4/H) * mat[i][j]) + ((1/H) * mat[i] [j + 1]);
        m[i][j + 1] = ((1/H) * mat[i][j - 1]) + ((4/H) * mat[i][j]) + ((1/H) * mat[i] [j + 1]);
    }

    print_matrice(m);
    printf("\n");
}
int main(void)
{

        int columns,rows,*mat;

        printf("Enter the number of columns.\n");
        scanf("%d", &columns);

        printf("Enter the number of rows.\n");
        scanf("%d", &rows);

        mat = create_matrice(columns,rows);

        random_init(mat,columns,rows);

        print_matrice(mat,columns,rows);

        free_mat(mat);

        return 0;

}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
	double **B, **C, *tmp;
	int PID, p_size,i, j, k;
	int row_chunk, offset, chunk;

	MPI_Init(NULL, NULL);
	MPI_Comm_size(MPI_COMM_WORLD, &p_size); 
	MPI_Comm_rank(MPI_COMM_WORLD, &PID);

	tmp = (double *) malloc (sizeof(double ) * N * N);
	B = (double **) malloc (sizeof(double *) * N);
	for (i = 0; i < N; i++)
		B[i] = &tmp[i * N];
	
	if (PID == 0) 
	{
		tmp = (double *) malloc (sizeof(double) * N * N);
		C = (double **) malloc (sizeof(double *) * N);
		for (i = 0; i < N; i++)
			C[i] = &tmp[i * N];

		//generate matrice B, if is master process
		for (i = 0; i < N; i++)
		 {
			for (j = 0; j < N; j++)
			 {
				B[i][j] = rand() % RANGE;
			}
		}
/*		//print matrice  B
		printf("matrice B: \n");
		print_matrice(B);
*/	}
	else 
	{
		tmp = (double *) malloc (sizeof(double) * N * N / p_size);
		C = (double **) malloc (sizeof(double *) * N / p_size);
		for (i = 0; i < N / p_size; i++)
			C[i] = &tmp[i * N];
	}

	row_chunk = N / p_size;
	//Broadcast B
	MPI_Bcast(B[0], N * N, MPI_DOUBLE, 0, MPI_COMM_WORLD);

	if(PID == 0)
		chunk = N - (p_size -1) * row_chunk;	
	else
		chunk = row_chunk;

	//initialize C
	for (i = 0; i < chunk; i++) 
	{
		for (j = 0; j < N; j++)
		{
			C[i][j] = 0.0;
		}
	}



	
	//calculation
	if(PID == 0)
	{
		for (i = EDGE; i < chunk; i++) 
		{
			for (j = EDGE; j < N - EDGE; j++)
			{
				double sum = 0;
				int m, n;
				for(m = i - EDGE; m <= i + EDGE; m++)
				{
					for(n = j - EDGE; n <= j + EDGE; n++)
					{
						sum += B[m][n];
					}
					
				}
				sum = sum / (2 * EDGE + 1) /(2 * EDGE + 1);
				C[i][j] = sum;
			}
		}
	}
	else if(PID == p_size - 1)
	{
		for (i = N - chunk; i < N - EDGE; i++) 
		{
			for (j = EDGE; j < N - EDGE; j++)
			{
				double sum = 0;
				int m, n;
				for(m = i - EDGE; m <= i + EDGE; m++)
				{
					for(n = j - EDGE; n <= j + EDGE; n++)
					{
						sum += B[m][n];
					}
					
				}
				sum = sum / (2 * EDGE + 1) /(2 * EDGE + 1);
				C[i - (N - chunk)][j] = sum;
			}
		}
	}
	else
	{
		int s_i = N - (p_size - 1) * row_chunk + chunk * (PID -1);
		for (i = s_i; i < s_i + chunk; i++) 
		{
			for (j = EDGE; j < N - EDGE; j++)
			{
				double sum = 0;
				int m, n;
				for(m = i - EDGE; m <= i + EDGE; m++)
				{
					for(n = j - EDGE; n <= j + EDGE; n++)
					{
						sum += B[m][n];
					}


				}
				sum = sum / (2 * EDGE + 1) /(2 * EDGE + 1);
				C[i - s_i][j] = sum;
			}
		}
	}	

	// master get result from slaves
	if (PID == 0) 
	{
		offset = chunk; 
		for (i = 1; i < p_size; i++) 
		{
			MPI_Recv(C[offset], row_chunk * N, MPI_DOUBLE, i, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
			offset += row_chunk;
		}
	}
	else
	{ 
		MPI_Send(C[0], row_chunk * N, MPI_DOUBLE, 0, TAG, MPI_COMM_WORLD);
	}
	// print result matrice C
	if (PID == 0)
	{
		//fill matrice C 's edge
		for(i = 0; i < N; i++)	
		{
			for(j = 0; j < N; j++)
			{
				if(i < EDGE || i >= N - EDGE || j < EDGE || j >= N - EDGE)
					C[i][j] = B[i][j];
			}
		}

		printf("matric C: \n");
		print_matrice(C);
	}

	MPI_Finalize();
	return 0;
}
Ejemplo n.º 5
0
int main (int argc, char *argv[])
{
    extern char *optarg;
    extern int optind, optopt;
    int opt=0;
    int time_simulation = 10000;
    float **t, **t1;
    int exec_time_flag = 1;
    int* sizes;
    int sizes_length = 0;
    int actual_size;
    int problem_size;
    int print_flag = 0;
    int clock_flag;
    time_t user_t_start;
    time_t user_t_end;
    clock_t clock_debut;
    clock_t clock_end;

    float* user_times;
    float* clock_times;

    struct rusage* rusg;

while((opt = getopt(argc, argv, "i:s:t:e:amM")) != -1) {
             switch (opt) {
           case 'i':
            time_simulation = atoi(optarg);
               break;
           case 's':
           	sizes_length = strlen(optarg);
           	sizes = malloc(sizeof(int) * sizes_length);
           	for(int j = 0; j < sizes_length; j++)
           		 sizes[j] = (optarg[j] - '0');
               break;
           case 't':
               break;
           case 'e':
               break;
           case 'a':
            print_flag = 1;
               break;
           case 'm':
           clock_flag = 1;
           exec_time_flag = 10;
           clock_times = malloc(sizeof(float)*exec_time_flag);
              break;
           case 'M':
            exec_time_flag = 10;
            user_times = malloc(sizeof(float)*exec_time_flag);
              break;
               case ':':       // -f or -o without operand
                       fprintf(stderr,
                               "Option -%c requires an operand\n", optopt);

                       break;
           case '?':
                       fprintf(stderr,
                               "Unrecognised option: -%c\n", optopt);

           }
     }
     /*
     if (errflg) {
         fprintf(stderr, "usage: . . . ");
         exit(2);
     }*/


     for(int s=0;s<sizes_length;s++)
     {
      problem_size = (sizes[s]+3); // equivalent a n-1 (retrait de 1 pour decalage de bit)
      actual_size = (2<<problem_size)+2; // calcul de la taille reelle de la matrice



     for(int i=0;i<exec_time_flag;i++)
     {
       rusg = malloc(sizeof(struct rusage));

       if(exec_time_flag > 1)
         user_t_start = time(NULL);

       if(clock_flag)
         clock_debut = clock();

          t = malloc(sizeof(float*)*actual_size);
          t1 = malloc(sizeof(float*)*actual_size);


       for(int i=0;i<actual_size;i++)
       {
         t[i] = malloc(sizeof(float)*actual_size);
         t1[i] = malloc(sizeof(float)*actual_size);
       }

        if(print_flag && i == 0)
          print_matrice(sizes[s],t1);

     fill_heatzone(problem_size,TEMP_CHAUD,t);
     initialize_extern_heat_x(actual_size,TEMP_FROID,t);
     initialize_extern_heat_y(actual_size,TEMP_FROID,t);
     fill_heatzone(problem_size,TEMP_CHAUD,t1);
     initialize_extern_heat_x(actual_size,TEMP_FROID,t1);
     initialize_extern_heat_y(actual_size,TEMP_FROID,t1);
     launch_diff(TEMP_CHAUD,time_simulation,problem_size,t,t1);
    if(print_flag && i == exec_time_flag -1)
       print_matrice(sizes[s],t1);
     free(t);
     free(t1);



     if(clock_flag)
     {
       clock_end = clock();
       clock_times[i] = (float)(clock_end - clock_debut)/CLOCKS_PER_SEC;
      printf("%f\n",(float)(clock_end - clock_debut)/CLOCKS_PER_SEC);
     }
     if(exec_time_flag > 1)
     {
         user_t_end = time(NULL);
         user_times[i] = difftime(user_t_end,user_t_start);
       printf("le temps de réponse utilisateur est de %.3f secondes\n", difftime(user_t_end,user_t_start));
     }
     getrusage(RUSAGE_SELF,rusg);
     printf("memoire consomé %ld kb\n",rusg->ru_maxrss);
     free(rusg);

     }





     if(clock_flag)
        printf("temps moyen (CPU) : %f\n",average_time(clock_times));

     if(exec_time_flag > 1)
       printf("temps moyen de reponse : %f\n",average_time(user_times));

   }

   	return 0;
 }
Ejemplo n.º 6
0
void RT_firstOrder(Pnum* m,Pnum* velocity,unsigned nb_row, unsigned nb_col, unsigned* f, unsigned nb_points_front){

    printf("Algorithm Rouy-Tourin : problem %d x %d with finite difference fisrt order\n",nb_row,nb_col);
    FILE *ft = gnudata_open("time_fo");
    FILE *fiter = gnudata_open("iter_fo");
    double time_start = give_time(), time_end = 0;
    Pnum* m_0 = (Pnum*) malloc(nb_col*nb_row*sizeof(Pnum));

    
    //Initialisation des noeuds
    int i,j,nb_iter = 0,convergence = 1;
    for (i = 0; i<nb_row; i++) {
        for (j = 0; j<nb_col; j++) {
            m_0[ind(nb_col,i,j)] = INF;
        }
    }
    
    //Initialisation des noeuds sources
    for (i = 0; i<2*nb_points_front; i+=2) {
        m_0[ind(nb_col,f[i], f[i+1])] = 0.0;
    }
    
    do{
        convergence = 1;
        copie(m_0, m, nb_row, nb_col);
        for (i = 0; i<nb_row; i++) {
            for (j = 0; j<nb_col; j++) {
                if (isInObstacle(velocity, nb_col, i,j)){
                    m_0[ind(nb_col, i, j)] = INF;
                    continue;
                }
                Pnum x_m1 = (i-1 >= 0)       ? m[ind(nb_col, i-1, j)] : INF;
                Pnum x_p1 = (i+1 < nb_col)  ? m[ind(nb_col, i+1, j)] : INF;
                Pnum y_m1 = (j-1 >= 0)       ? m[ind(nb_col, i, j-1)] : INF;
                Pnum y_p1 = (j+1 < nb_row)  ? m[ind(nb_col, i, j+1)] : INF;
                Pnum Tx = fminf(x_m1, x_p1);
                Pnum Ty = fminf(y_m1, y_p1);
                Pnum f = velocity[ind(nb_col, i, j)];
                Pnum sol = solveEquation_1(Tx,Ty,h,f);
                Pnum sol_min = fminf(m[ind(nb_col, i, j)],sol);
                m_0[ind(nb_col, i, j)] = sol_min;
                
                convergence = convergence && (fabsf( m[ind(nb_col,i, j)] - m_0[ind(nb_col, i, j)] ) <= epsilon);
            }
        }
        if (PRINT) {
            printf("\n----\n");
            print_matrice(m, nb_row, nb_col);
        }

        nb_iter++;
        
    } while (!convergence);
    
    time_end = give_time();
    dput_xy(ft, nb_row, time_end-time_start);
    dput_xy(fiter, nb_row, nb_iter);
    calculDistance(1,m, nb_col/2, nb_row/2, nb_row, nb_col);
    free(m_0);
    printf("%d itérations\n",nb_iter);
    printf("TIME : %f sec\n\n",time_end-time_start);
}