Ejemplo n.º 1
0
void levelSet3Dfast(MATRIXD im, MATRIX init_mask, int max_its, double E, double T, double alpha, 
					MATRIX *seg0, MATRIX *tmap, MATRIXD *phi, long **ls_vols, int offset){
	long dx = im.dx, dy = im.dy, dz = im.dz;
	int *px, *py, *pz, *qx, *qy, *qz, k, tmap_it = 1;
	MATRIX intTmp;
	MATRIX intTmp2 = allocateMatrix(dx,dy,dz);	// add a new temporary matrix
	MATRIXD dblTmp = allocateMatrixD(dx,dy,dz);	
	MATRIXD gradPhi = allocateMatrixD(dx,dy,dz);
	int minx, maxx, miny, maxy, minz, maxz;
	
	*phi = allocateMatrixD(dx,dy,dz);
	*seg0 = allocateMatrix(dx,dy,dz);
	*tmap = allocateMatrix(dx,dy,dz);
	*ls_vols = (long *)calloc(max_its,sizeof(long)); // initialized with 0
	
	initializeMatrix(tmap,0);
	initializeMatrix(seg0,0);		// initialized with 0
	initializeMatrix(&intTmp2,0);	// initialized with 0
	initializeGlobals4bwdist(dx,dy,dz,&px,&py,&pz,&qx,&qy,&qz,&intTmp);
	
	createSignedDistanceMap(init_mask,phi,px,py,pz,qx,qy,qz,intTmp,dblTmp); // temps: intTmp, dblTmp

	findLimits(init_mask,offset,&minx,&maxx,&miny,&maxy,&minz,&maxz);
	
	for (k = 1; k <= max_its; k++){
		calculateCurvature(*phi,&dblTmp,minx,maxx,miny,maxy,minz,maxz); // dblTmp keeps the curvatures
		calculateF(im,dblTmp,&dblTmp,E,T,alpha,minx,maxx,miny,maxy,minz,maxz); // first dblTmp (input): curvatures, second dblTmp (output): F values
		calculateGradPhi(*phi,dblTmp,&gradPhi,minx,maxx,miny,maxy,minz,maxz);
		evolveCurve(phi,dblTmp,gradPhi,minx,maxx,miny,maxy,minz,maxz);
		
		if (k % 50 == 0)
			reinitializeDistanceFunction(phi,init_mask,px,py,pz,qx,qy,qz,intTmp,dblTmp); // temps: init_mask, intTmp, dblTmp
		
		(*ls_vols)[k-1] = selectNonpositive(*phi,&intTmp2,minx,maxx,miny,maxy,minz,maxz); // use the second temporarymatrix
		
		if (k == 1){
			selectNonpositive(*phi,seg0,minx,maxx,miny,maxy,minz,maxz);
			copyMatrixWithLimits(tmap,*seg0,minx,maxx,miny,maxy,minz,maxz);
			findLimits(*tmap,offset,&minx,&maxx,&miny,&maxy,&minz,&maxz);
		}
		else if (k % 10 == 0){
			selectNonpositive(*phi,&intTmp2,minx,maxx,miny,maxy,minz,maxz);			// use the second temporarymatrix
			updateMaps(tmap,intTmp2,*seg0,tmap_it,minx,maxx,miny,maxy,minz,maxz);	// use the second temporarymatrix
			copyMatrixWithLimits(seg0,intTmp2,minx,maxx,miny,maxy,minz,maxz);		// use the second temporarymatrix
			tmap_it++;
			
			findLimits(*tmap,offset,&minx,&maxx,&miny,&maxy,&minz,&maxz);
		}
	}
	selectNonpositive(*phi,seg0,0,dx-1,0,dy-1,0,dz-1);
	//selectNonpositive(*phi,seg0,minx,maxx,miny,maxy,minz,maxz); ???? (consider this modification if there are more problems)
	
	freeMatrixD(gradPhi);
	freeMatrixD(dblTmp);
	freeMatrix(intTmp2);
	freeGlobals4bwdist(px,py,pz,qx,qy,qz,intTmp);
}
Ejemplo n.º 2
0
/**
	Initializes the Matrix.

	@param width - the width of the matrix.
	@param height - the height of the matrix.
	@param value - the value to initialize every cell in the matrix.
*/
Matrix::Matrix(int width, int height, int value) {
	_matrix = vector<int>();

	setWidth(width);
	setHeight(height);

	if (value != -1)
	initializeMatrix(value);
}
Ejemplo n.º 3
0
int main(int argc, char **argv) {
	readConsoleValues(argc, argv);
	//NUM_THREADS = 6;
	//MATRIX_SIZE = 4;
	matrixA.size = MATRIX_SIZE;
	matrixResult.size = MATRIX_SIZE;

	constructSquareMatrix(&matrixA);
	constructSquareMatrix(&matrixResult);
	initializeMatrix(&matrixA, -1);
	initializeMatrix(&matrixResult, 0);

	#pragma omp parallel shared(nextRowToCalc) num_threads(NUM_THREADS)
	run();

	//printMatrix(matrixA);
	//printMatrix(matrixResult);
	destroyMatrix(matrixA);
	destroyMatrix(matrixResult);
	printf("Main process [%i] finished\n", getpid());
	exit(0);
}
Ejemplo n.º 4
0
void levelSet3D(MATRIXD im, MATRIX init_mask, int max_its, double E, double T, double alpha, 
				MATRIX *seg0, MATRIX *tmap, MATRIXD *phi, long **ls_vols){
	// some of these variables will be used as the local variables for other functions
	// they are defined outside (like global variables) because we want to decrease the overhead
	// corresponding to allocation and deallocation (since we have multiple calls for the same function
	long dx = im.dx, dy = im.dy, dz = im.dz;
	int *px, *py, *pz, *qx, *qy, *qz, k, tmap_it = 1;
	MATRIX intTmp;
	MATRIXD dblTmp = allocateMatrixD(dx,dy,dz);	
	MATRIXD gradPhi = allocateMatrixD(dx,dy,dz);
	
	*phi = allocateMatrixD(dx,dy,dz);
	*seg0 = allocateMatrix(dx,dy,dz);
	*tmap = allocateMatrix(dx,dy,dz);
	*ls_vols = (long *)calloc(max_its,sizeof(long)); // initialized with 0
	
	initializeMatrix(tmap,0);
	initializeGlobals4bwdist(dx,dy,dz,&px,&py,&pz,&qx,&qy,&qz,&intTmp);

	createSignedDistanceMap(init_mask,phi,px,py,pz,qx,qy,qz,intTmp,dblTmp); // temps: intTmp, dblTmp

	for (k = 1; k <= max_its; k++){
		calculateCurvature(*phi,&dblTmp,0,dx-1,0,dy-1,0,dz-1); // dblTmp keeps the curvatures
		calculateF(im,dblTmp,&dblTmp,E,T,alpha,0,dx-1,0,dy-1,0,dz-1); // first dblTmp (input): curvatures, second dblTmp (output): F values
		calculateGradPhi(*phi,dblTmp,&gradPhi,0,dx-1,0,dy-1,0,dz-1);
		evolveCurve(phi,dblTmp,gradPhi,0,dx-1,0,dy-1,0,dz-1);
		
		if (k % 50 == 0)
			reinitializeDistanceFunction(phi,init_mask,px,py,pz,qx,qy,qz,intTmp,dblTmp); // temps: init_mask, intTmp, dblTmp
		
		(*ls_vols)[k-1] = selectNonpositive(*phi,&intTmp,0,dx-1,0,dy-1,0,dz-1);
		
		if (k == 1){
			selectNonpositive(*phi,seg0,0,dx-1,0,dy-1,0,dz-1);
			copyMatrix(tmap,*seg0);
		}
		else if (k % 10 == 0){
			selectNonpositive(*phi,&intTmp,0,dx-1,0,dy-1,0,dz-1);
			updateMaps(tmap,intTmp,*seg0,tmap_it,0,dx-1,0,dy-1,0,dz-1);
			copyMatrix(seg0,intTmp);
			tmap_it++;
		}
	}
	selectNonpositive(*phi,seg0,0,dx-1,0,dy-1,0,dz-1);

	freeMatrixD(gradPhi);
	freeMatrixD(dblTmp);
	freeGlobals4bwdist(px,py,pz,qx,qy,qz,intTmp);
}
Ejemplo n.º 5
0
void Matrix::allocate(const unsigned int rows, const unsigned int cols, const ValueType valueType)
{
    if(m_data)
    {
        delete [] m_data;
        m_data = 0;
    }
    
    unsigned int valueSize = Matrix::valueSize(valueType);
    unsigned int bufferSize = rows * cols * valueSize;
    m_data = new uint8_t[bufferSize];
    
    setBuffer(m_data, bufferSize);
    initializeMatrix(rows, cols, cols * valueSize, m_data, valueType);
}
Ejemplo n.º 6
0
static int PositionWeightMatrix_init( PositionWeightMatrix* self, PyObject* args, PyObject* kwds )
{
	PyObject* listObj;

	static char *kwlist[] = { "matrix", NULL };
	
	if( !PyArg_ParseTupleAndKeywords( args, kwds, "O!", kwlist, &PyList_Type, &listObj ) )
		return -1;
	
	if( listObj )
	{
		return initializeMatrix( self, listObj );
	}
	
	//arg is not list, so error
	return -1;
	
}
Ejemplo n.º 7
0
void solveProblem1(int numberOfAlterations, int initializationValue)
{
	long long message[3];

	if (procRank == 0)
	{
		long long matrix[8][8];
		initializeMatrix(matrix);
		
		while (!matrixIsComplete(matrix))
		{
			// Receive matrix coordinates along with value
			MPI_Recv(message, 3, MPI_LONG_LONG, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
			matrix[message[0]][message[1]] = message[2];
		}
		
		printMatrix(matrix);
	}
	else
	{
		int i, j;
		
		for (i = procRank - 1; i <= 64; i += procCount - 1)
		{
			long long value = initializationValue;
			int x = i / 8;
			int y = i % 8;
			
			for (j = 1; j <= numberOfAlterations; j++)
			{
				usleep(1000);
				value += (x + y) * j;
			}
			
			message[0] = x;
			message[1] = y;
			message[2] = value;
			
			MPI_Send(message, 3, MPI_LONG_LONG, 0, 0, MPI_COMM_WORLD);
		}
	}
}
Ejemplo n.º 8
0
void reallocateMatrix(MATRIX *M, long row, long column){
	int i, j, minrow, mincol;
	MATRIX tmp = allocateMatrix(M->row,M->column);

	copyMatrix(&tmp,*M);
	freeMatrix(*M);

	*M = allocateMatrix(row,column);
	initializeMatrix(M,0);

	if (tmp.row > row)			minrow = row;
	else						minrow = tmp.row;
	
	if (tmp.column > column)	mincol = column;
	else						mincol = tmp.column;
	
	for (i = 0; i < minrow; i++)
		for (j = 0; j < mincol; j++)
			M->data[i][j] = tmp.data[i][j];
	freeMatrix(tmp);
}
Ejemplo n.º 9
0
int main() {
	int** grid = initializeMatrix();
	int score, force_exit, actualSelection = -1;
	st = GetStdHandle(STD_OUTPUT_HANDLE);
	restore_colorProfile(0);
	allocateTail(&(Snake.tail), NULL,  0);
	while (actualSelection != 0) {
		mainMenuGraphics();
		actualSelection = mainMenu(1);
		if (actualSelection == 1) {
			actualSelection = 0;
			graphics_SelectGameMode();
			while ((actualSelection != 1) && (actualSelection != 2)) {
				actualSelection = mainMenu(2);
			}
			int gameMode = graphics_levelSelect();
			initializeNewGame(grid, &score, &force_exit);
			insertTitle();
			drawInGameBorder();
			displayPauseInstructions();
			drawSnake(0, grid);
			updateScore(score);
			assignSnake2Matrix(grid, 1);
			addFood(grid);
			newGame(grid, &score, &force_exit, actualSelection - 1, gameMode);
			if (!force_exit) {
				_getch();
			}
			clear_screen();
			restoreSnakeLength();
		}
		else if (actualSelection == 2) {
			instructions();
		}
	}
	return 0;
}
Ejemplo n.º 10
0
void master() {
  
  double *a;
  double *rhs;
  int ntasks,pid;
  double *buffer;
  double *workbuf;
  int i,j,k;
  MPI_Status status;
  time_t t0,t1;
  int  ct;
 
  /* get the number of the processes in application. */
  MPI_Comm_size(MPI_COMM_WORLD,&ntasks); 

  /* allocate matrix, rhs vector */
  a = (double *) malloc( matrix_size*matrix_size*sizeof(double) ) ;
  rhs = (double *) malloc( matrix_size*sizeof(double) ) ;

  /* initialize the matrix */
  /* Do we need to allocate a matrix a or ony a row 
 *  *      and initilize each row and then send to corresponding process? */
  initializeMatrix( matrix_size, a, rhs );

  /* Send each row to the corresponding process. */
  for (i=block_size;i<matrix_size;i+=block_size) {

    /* send i row to i+block_size-1 row to process 
 *  *        (i mod block_size)%ntasks. */
    pid=(i/block_size)%ntasks;

    if (pid!=0)
      MPI_Send(&a[i*matrix_size],matrix_size*block_size,MPI_DOUBLE,
	       pid,i,MPI_COMM_WORLD);
  }

  MPI_Barrier(MPI_COMM_WORLD);

  /* allocate buffer space, it should be big enough 
 *  *      to contaion a whole row of block. */
  buffer=(double *)malloc(matrix_size*block_size*sizeof(double));
  
  time(&t0);

  /* Do the computation work of process 0 */
  for (i=0;i<matrix_size;i+=block_size) {

    /* compute the id of the processor that own the row i 
 *  *        to row i+block_size-1 */
    pid=(i/block_size)%ntasks;

    if (pid==0) { /* matser process. Me! */
     
      /* factor diagonal */
      lu0(&a[i+i*matrix_size],block_size,matrix_size);

      /* modify "column" by diagonal */
      for (j=i+block_size;j<matrix_size;j+=block_size)
	bdiv(&a[j+i*matrix_size],&a[i+i*matrix_size],
	     block_size,matrix_size);

      /* send this row to other processes, only need to send 
 *  * 	 the column after diagonal? */
      for (j=1;j<ntasks;j++) {
	MPI_Send(&a[i*matrix_size],block_size*matrix_size,MPI_DOUBLE,
		 j,i,MPI_COMM_WORLD);
      }
      workbuf=&a[i*matrix_size];
    }
    else { /* other process */
      /* receive row i to row i+block_size-1 from process pid */
      MPI_Recv(&buffer,block_size*matrix_size,MPI_DOUBLE,pid,i,
	       MPI_COMM_WORLD,&status);
      workbuf=buffer;
    }

    /* modify the "row" using diagonal */
    for (j=i+(ntasks-pid)*block_size;j<matrix_size;j+=block_size*ntasks) 
      bmodd(&a[i+j*matrix_size],&workbuf[i],
	    block_size,matrix_size);

    /* modify the internal rows and columns */
    for (j=i+(ntasks-pid)*block_size;j<matrix_size;j+=block_size*ntasks)
      for (k=i+block_size;k<matrix_size;k+=block_size) 
	bmod(&a[k+j*matrix_size],&workbuf[k],&a[i+j*matrix_size],
	     block_size,matrix_size);

  }

  MPI_Barrier(MPI_COMM_WORLD);

  time(&t1);
  ct=t1-t0;
  printf("LU decomposition took %d millisecs\n", ct);

  /* Receive the modified matrix from all other processes. */
  for (i=0;i<matrix_size;i+=block_size) {

    /* compute the id of the processor that own the row i 
 *  *        to row i+block_size-1 */
    pid=(i/block_size)%ntasks;

    if (pid!=0)
      MPI_Recv(&a[i*matrix_size],block_size*matrix_size,MPI_DOUBLE,pid,i,MPI_COMM_WORLD,&status);
  }
  
  /* test the resulting decoposition */
  checkResult(matrix_size,a,rhs);
  
}
Ejemplo n.º 11
0
int main() {
    for(int n=5; n<=200; n+=5) {
        printf("\n%d iterations\n", n);
        printf("---------------------------------\n");
        int t = 0;
        matrix A = initializeMatrix(n,n);
        Triangles* workers = new Triangles[n];

        Stopwatch timer;
        timer.start();
        for(int x = 0; x<n; x++) {
            workers[x].setParameters(x, n, A);
            if(!workers[x].StartInternalThread()) {
                printf("Err pthread_create for thread %d \n", x+1);
            }
        }

        for(int x=0; x<n; x++) {
            workers[x].WaitForInternalThreadToExit();
            t += workers[x].getResult();
        }
        t /= 3;
        timer.stop();

        float averageTime = [](Triangles* w, int n)->float {
            float sum = 0;
            for(int x = 0; x<n; x++) {
                sum += w[x].getTime();
            }
            return sum/n;
        }(workers, n);

        float maxTime = [](Triangles* w, int n)->float {
            float max = 0;
            for(int x = 0; x<n; x++) {
                if(w[x].getTime() > max) {
                    max = w[x].getTime();
                }
            }
            return max;
        }(workers, n);

        float minTime = [](Triangles* w, int n)->float {
            float min = w[0].getTime();
            for(int x = 1; x<n; x++) {
                if(w[x].getTime() < min) {
                    min = w[x].getTime();
                }
            }
            return min;
        }(workers, n);

        float linearTime = [](Triangles* w, int n)->float {
            float sum = 0;
            for(int x = 0; x<n; x++) {
                sum += w[x].getTime();
            }
            return sum;
        }(workers, n);

        float overallTime = timer.getTime();

        printf("All tasks ended succesfully\n");
        printf("Found %d triangles \n", t);
        printf("Average search time:\t %.0f \n", averageTime);
        printf("Max search time:\t %.0f \n", maxTime);
        printf("Min search time:\t %.0f \n", minTime);
        printf("Linear approximate time: %.0f \n", linearTime);
        printf("Overall search time:\t %.0f\n", overallTime);
        printf("---------------------------------\n");
    }
    getchar();

    return 0;
}
//Parallel matrix constructor
ParallelBooleanMatrix::ParallelBooleanMatrix(int rows, int columns) : BooleanMatrix(rows, columns) {
	initializeMatrix();
}
//Serial matrix constructor
SerialBooleanMatrix::SerialBooleanMatrix(int rows, int columns) : BooleanMatrix(rows, columns) {
	initializeMatrix();
}
Ejemplo n.º 14
0
void solveProblem2(int numberOfAlterations, int initializationValue)
{
	long long message[9];
	if (procRank == 0)
	{
		long long matrix[8][8];
		initializeMatrix(matrix);
		
		while (!matrixIsComplete(matrix))
		{
			// Receive row number and all values for that row
			MPI_Recv(message, 9, MPI_LONG_LONG, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
			
			int i;
			for (i = 0; i < 8; i++)
			{
				matrix[message[0]][i] = message[i+1];
			}
		}
		
		printMatrix(matrix);
	}
	else if (procRank <= 16)
	{
		long long row[8];
		long long previousRow[numberOfAlterations + 1];
		int rowNum = (procRank - 1) / 2;
		int i, j;
		
		for (i = 0; i < 8; i++)
			row[i] = initializationValue;
			
		for (i = 0; i <= numberOfAlterations; i++)
			previousRow[i] = initializationValue;
			
		if (procRank % 2 == 1)
		{
			for (j = 0; j < 8; j++)
			{
				for (i = 1; i <= numberOfAlterations / 2; i++)
				{
					usleep(1000);
					if (j == 0)
						row[0] += rowNum * i;
					else
						row[j] += previousRow[i] * i;
						
					previousRow[i] = row[j];
				}
				
				message[0] = row[j];
				MPI_Send(message, 1, MPI_LONG_LONG, procRank + 1, 0, MPI_COMM_WORLD);
			}
		}
		else
		{
			for (j = 0; j < 8; j++)
			{
				MPI_Recv(message, 1, MPI_LONG_LONG, procRank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
				row[j] = message[0];
				for (i = numberOfAlterations / 2 + 1; i <= numberOfAlterations; i++)
				{
					usleep(1000);
					if (j == 0)
						row[0] += rowNum * i;
					else
						row[j] += previousRow[i] * i;
						
					previousRow[i] = row[j];
				}
			}
			
			message[0] = rowNum;
			for (i = 0; i < 8; i++)
				message[i+1] = row[i];
				
			MPI_Send(message, 9, MPI_LONG_LONG, 0, 0, MPI_COMM_WORLD);
		}
	}
}
int main(int argc, char** argv){
	int opt;	
	int* s;//listes des tailles
	int* e;//liste des etapes
	int* t = NULL;//liste des nombres de threads
	
	// Declaration des options
	int i = 0;
	int m = 0, M = 0, a = 0;

	int nbProblems = 0;
	int nbEtapes = 0;
	int nbDifferentThreads = 0;

	// Recuperer les options du programme
	while ((opt = getopt(argc,argv,"s:mMai:e:t:")) != -1){
		switch(opt){
			// [0-9] : taille du probleme a executer, 2**(s+4) cases sur une ligne
			case 's':
				// Verifier si l'option contient des valeurs non autorisees
				if (atoi(optarg) == 0) {
					optarg = "0";
				} 
				// Recuperer le nombre de problemes a traiter et leur taille, puis initialiser le tableau
				nbProblems = strlen(optarg);
				if( (s = malloc(sizeof(int) * nbProblems)) == NULL){
					fprintf(stderr,"Allocation impossible \n");
					exit(EXIT_FAILURE);
				}
				int j = 0;
				for(j = 0; j < nbProblems; j++) {
					s[j] = optarg[j] - '0';
				}
				break;
			
			// mesure et affichage du temps d'execution (consommation du CPU),
			//	suppression de toute trace d'execution
			case 'm':
				// on executera 10 fois chaque scenario puis on eliminera les deux
				//	extremes pour faire la moyenne des 8 executions restantes
				m = 1;
				if (a) {
					a = 0;
				}
				break;

			// mesure et affichage du temps d'execution (temps de reponse utilisateur),
			//	suppression de tout trace d'executuion
			case 'M':
				// on executera 10 fois chaque scenario puis on eliminera les deux
				//	extremes pour faire la moyenne des 8 executions restantes
				M = 1;
				if (a) {
					a = 0;
				}
				break;

			// affichage de la temperature initiale et de la temperature finale pour les indices
			//	correspondant au quart superieur gauche du tableau, pour les indices modulo 2**s
			case 'a':
				// par defaut : on n'affiche rien
				if (!(m || M)) {
					a = 1;
				}
				break;

			// number : nombre d'iteration a executer
			case 'i':
				// par defaut 10 000 iterations
				i = atoi(optarg);
				if (i == 0) {
					i = 10000;
				}
				break;

			// [0-5] : etape du programme a executer
			case 'e':
				// 0 : iteratif
				// 1 : thread avec barriere Posix
				// 2 : thread avec barriere implementee avec variable condition
				// 3 : thread avec barriere implementee avec semaphore
				// 4 : programme OpenCL calcul sur CPU
				// 5 : programme OpenCL calcul sur GPU
								// Verifier si l'option contient des valeurs non autorisees
				if (atoi(optarg) == 0) {
					optarg = "0";
				} 
				// Recupere les etapes du programme a executer
				nbEtapes = strlen(optarg);
				
				if( (e = malloc(sizeof(int) * nbEtapes)) == NULL){
					fprintf(stderr,"Allocation impossible \n");
					exit(EXIT_FAILURE);
				}

				int k = 0;
				for(k = 0; k < nbEtapes; k++) {
					e[k] = optarg[k] - '0';
				}
				break;

			// [0-5] : nombre de thread a creer
			case 't':
				// 4**t thread a creer, t varie entre 0 (iteratif) et 5 (1024 threads)
				// concerne bien evidemment les etapes 1 a 5, sans effet sur etape 0
				nbDifferentThreads = strlen(optarg);
				if( (t = malloc(sizeof(int) * nbDifferentThreads)) == NULL){
					fprintf(stderr,"Allocation impossible \n");
					exit(EXIT_FAILURE);
				}
				int l;
				for(l=0;l<nbDifferentThreads;l++){
					t[l] = optarg[l] - '0';
				}
				break;
		}
	}

	int j, k, l;
	int size = 0;
	float** mat1 = NULL;
	float** mat2;
	int iteration = 0;
	int n = 0;


	time_t debutTemp, finTemp;
	clock_t start_t, end_t;

	//iteration sur le nombre d'etapes a executer (option -e)
	for(l = 0 ; l<nbEtapes ; l++){
		etapeEnCours=e[l];//sauvegarde de l'etape en cours.
		// Iteration sur le nombre de problemes a lancer (option -s)
		for (j = 0; j < nbProblems; j++) {
			int indexThread;
			int numberOfThreads=0;
			int endAllThreads=nbDifferentThreads;
			int ThreadOk = 1;
			if(t==NULL || e[l]==0){endAllThreads=1;ThreadOk=0;}//pour passer une fois dans la boucle for.
			for(indexThread=0;indexThread<endAllThreads;indexThread++){
				n = s[j] + 4;
				size = (1 << n) + 2;
				// Allocation memoire des matrices a utiliser
				if( (mat1 = malloc(sizeof(float*) * size)) == NULL){
					fprintf(stderr,"Allocation impossible \n");
					exit(EXIT_FAILURE);
				}
				
				for (k = 0; k < size; k++) {
					if( (mat1[k] = malloc(sizeof(float) * size)) == NULL){
						fprintf(stderr,"Allocation impossible \n");
						exit(EXIT_FAILURE);
					}
				}
				
				if( (mat2 = malloc(sizeof(float*) * size)) == NULL){
					fprintf(stderr,"Allocation impossible \n");
					exit(EXIT_FAILURE);
				}
				for (k = 0; k < size; k++) {
					if( (mat2[k] = malloc(sizeof(float) * size)) == NULL){
						fprintf(stderr,"Allocation impossible \n");
						exit(EXIT_FAILURE);
					}
				}

				// Initialisation des matrices
				defineConstants(size, n);
				initializeMatrix(*mat1, size);
				initializeMatrix(*mat2, size);
				int counter = 1;
				float* times;
				float* clocks;
				// Si l'option m ou M a ete entree, le compteur est mis a 10 pour calculer la moyenne de temps
				if (m) {
					counter = 10;
					if( (clocks = malloc(sizeof(float) * counter)) == NULL){
						fprintf(stderr,"Allocation impossible \n");
						exit(EXIT_FAILURE);
					}
				}
				
				if (M) {
					counter = 10;
					if( (times = malloc(sizeof(float) * counter)) == NULL){
						fprintf(stderr,"Allocation impossible \n");
						exit(EXIT_FAILURE);
					}
				}
				if(ThreadOk){
					numberOfThreads = (1 << (2*t[indexThread]));//pow(4,t[indexThread]);
				}
				for (k = 0; k < counter; k++) {
					if (m) {
						start_t = clock();
					}
					if (M) {
						debutTemp = time(NULL);
					}
					switch(e[l]){
						case 0:
							//printf("Execution de l'etape 0 ...[size matrix : %d]\n",size-2);
							if (a) {
								printf("VALEUR INITIAL\n");
								printMatrix(*mat1,size);
							}
							for (iteration = 0; iteration < i; iteration++) {
								nextStep(*mat1, *mat2, size);
							}
							if(a){
								printf("VALEUR FINALE\n");
								printMatrix(*mat1,size);
							}
						break;

						case 1:
							//printf("Execution de l'etape 1 ...[size matrix : %d, nb Threads : %d]\n",size-2,numberOfThreads);
							if(a){
								printf("AVANT\n");
								printMatrix(*mat1,size);
							}
							tab = decoupageMatrice(numberOfThreads,size-2);
							for (iteration = 0; iteration < i; iteration++) {
								nextStepBarrier(*mat1, *mat2, size, numberOfThreads);
							}
							if(a){
								printf("APRES\n");
								printMatrix(*mat1,size);
							}
						break;

						case 2:
							//printf("Execution de l'etape 2 ...[size matrix : %d, nb Threads : %d]\n",size-2,numberOfThreads);
							if(a){
								printf("AVANT\n");
								printMatrix(*mat1,size);
							}
							tab = decoupageMatrice(numberOfThreads,size-2);
							for (iteration = 0; iteration < i; iteration++) {
								nextStepBarrier(*mat1, *mat2, size, numberOfThreads);
							}
							if(a){
								printf("APRES\n");
								printMatrix(*mat1,size);
							}
						break;

						case 3:
							//printf("Execution de l'etape 3 ...[size matrix : %d, nb Threads : %d]\n",size-2,numberOfThreads);
							if(a){
								printf("AVANT\n");
								printMatrix(*mat1,size);
							}
							tab = decoupageMatrice(numberOfThreads,size-2);
							for (iteration = 0; iteration < i; iteration++) {
								nextStepBarrier(*mat1, *mat2, size, numberOfThreads);
							}
							if(a){
								printf("APRES\n");
								printMatrix(*mat1,size);
							}
						break;

						case 4:
							//printf("Execution de l'etape 4 ...\n");
						break;
						case 5:
							//printf("Execution de l'etape 5 ...\n");
						break;					

						default:
							//ne rien faire
							printf("Veuillez entrer une valeur correcte pour l'option -e\n");
						break;
					}
					if (m) {
						end_t = clock();
						clocks[k] = (float)(end_t - start_t) / CLOCKS_PER_SEC;
					}
					if (M) {
						finTemp = time(NULL);
						times[k] = (float)(finTemp - debutTemp);				
					}
				}

				if (m) {
					if(ThreadOk){
						printf("Average clock for s = %d e = %d t = %d : %f\n", s[j], e[l], t[indexThread], getAverageClockWithoutExtremes(clocks, counter));
					}
					else{
						printf("Average clock for s = %d e = %d : %f\n", s[j], e[l], getAverageClockWithoutExtremes(clocks, counter));
					}
					free(clocks);
				}
				if (M) {
					if(ThreadOk){
						printf("Average time for s = %d e = %d t = %d : %f\n", s[j], e[l], t[indexThread], getAverageClockWithoutExtremes(times, counter));
					}
					else{
						printf("Average time for s = %d e = %d : %f\n", s[j], e[l], getAverageClockWithoutExtremes(times, counter));
					}
					free(times);
				}
				
			}
		}
	}

	return 0;
}
Ejemplo n.º 16
0
int main(int argc, char* argv[]) {

    int myrank;
    int nodes;
    double *a;
    int nbDims = 2; 
    int dims[2];


    int i, j, k, noTest;
    Arguments arguments;

    arguments.matrix_size = -1;
    arguments.matrice = arguments.solution = NULL;
    arguments.imprimer = 0;
    arguments.pivot = 0;
    arguments.write = NULL;

    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
    MPI_Comm_size(MPI_COMM_WORLD, &nodes);

    for(i = 1; i < argc; i++) {
        if(argv[i][0] == '-') {
            switch(argv[i][1]) {
                case 'm': if(i+1 < argc) {
                              arguments.matrix_size = atoi(argv[++i]);
                          }
                          break;
                case 'f': if(i+1 < argc) {
                              arguments.matrice = argv[++i];
                          }
                          break;
                case 's': if(i+1 < argc) {
                              arguments.solution = argv[++i];
                          }
                          break;
                case 'p': arguments.pivot = 1;
                          break;
                case 'i': arguments.imprimer = 1;
                          break;
                case 'x': if(i+1 < argc) {
                             dims[0] = atoi(argv[++i]);
                          }
                         break; 
                case 'y': if(i+1 < argc) {
                             dims[1] = atoi(argv[++i]);
                          }
                         break; 
                case 'w': if(i+1 < argc) {
                              arguments.write = argv[++i];
                          }
                          break;
                default: fprintf(stderr, "Usage: %s -m <tailleMatrice> "\
                                 "-x <lignes> -y <colonnes> [-w nomFichier]" \
                                 "[-f nomFichier] [-s solution]\n", argv[0]);
                         exit(EXIT_FAILURE);
            }
        }
    }

    if(arguments.matrix_size < 0) {
        fprintf(stderr,"Erreur: vous devez fournir une taille de matrice.\n");
        exit(EXIT_FAILURE);
    }

    if((arguments.matrix_size % nodes != 0) || (nodes % dims[0] !=0)
            || (nodes % dims[1] !=0)) {
        fprintf(stderr, "Erreur: le nombre de noeuds doit diviser la"\
                "et les colonnes et lignes doivent diviser le nombre de noeuds .\n");
        exit(EXIT_FAILURE);
    }

    //On part la minuterie
    double tempsEcoule = -MPI_Wtime(); 

    //On crée la topologie
    int period[] = {1,0};
    int reorder = 1;
    int coord[2]; //Pour ma position
    MPI_Comm comm;

    MPI_Cart_create(MPI_COMM_WORLD, nbDims, dims, period, reorder, &comm);

    //On imprime 
    MPI_Cart_coords(comm, myrank, nbDims, coord);

    //Maintenant on crée les matrices
    int nbLinesParBloc = arguments.matrix_size / dims[0];
    int nbColsParBloc = arguments.matrix_size / dims[1];

    if(arguments.matrice) {
        a = readMatrixCart(arguments.matrice, arguments.matrix_size, 
                nbColsParBloc, nbLinesParBloc, coord);
    } else {
        a = (double*)malloc(nbLinesParBloc*nbColsParBloc*sizeof(double));
        initializeMatrix(nbColsParBloc, nbLinesParBloc,a);
    }

    //On crée les communicateurs pour les colonnes et les lignes
    MPI_Comm lignes;
    MPI_Comm colonnes;
    int cols[2] = {1,0};
    int lines[2] = {0,1};

    MPI_Cart_sub(comm, lines, &lignes);
    MPI_Cart_sub(comm, cols,  &colonnes);

    factoriserLU(a, arguments.matrix_size, nbLinesParBloc, nbColsParBloc,
            lignes, colonnes, coord);

    //On fait la décomposition
    double tempsMax;
    tempsEcoule += MPI_Wtime();
    MPI_Reduce( &tempsEcoule, &tempsMax, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD );


    if(arguments.solution) {
        //on vérifie le résultat
        double *resultat =  readMatrixCart(arguments.solution, arguments.matrix_size, 
                nbColsParBloc, nbLinesParBloc, coord);
        checkResult(resultat, a, nbColsParBloc, nbLinesParBloc, MPI_COMM_WORLD);

        if(myrank==0) {
            printf("Resultat de la factorisation lu correcte.\n");
        }
        if(resultat) free(resultat);
    }

    if (myrank == 0) {
        printf( "Factorisation LU sans pivot effectuee en %.3f msecs\n", 
                1000*tempsEcoule);
    }

    if(arguments.imprimer || arguments.write) {

        //Si mon rang est 0 pour mon communicateur Lignes
        double *lines;
        if(coord[1] == 0) {
            lines = (double *)malloc(nbLinesParBloc*arguments.matrix_size*sizeof(double));
        }

        //On rebatit les lignes
        for(i = 0; i < nbLinesParBloc; i++) {
            MPI_Gather(&a[i*nbColsParBloc], nbColsParBloc, MPI_DOUBLE, 
                    &lines[i*arguments.matrix_size], nbColsParBloc, MPI_DOUBLE, 0, lignes);
        }

        double *matriceComplete;
        if(myrank ==0) { //Je suis la racine, rank ==0 aussi
            matriceComplete = (double *)malloc(arguments.matrix_size*
                    arguments.matrix_size*sizeof(double));
        }
        //On fait la communication
        if(coord[1] ==0){ //Je suis dans la colonne 0 -- à vérifier
            MPI_Gather(lines, arguments.matrix_size*nbLinesParBloc, 
                    MPI_DOUBLE, matriceComplete, arguments.matrix_size*
                    nbLinesParBloc, MPI_DOUBLE, 0, colonnes);
            free(lines);
        }

        if(myrank == 0) {
            if(arguments.imprimer) {
                printMatrix(arguments.matrix_size, arguments.matrix_size, 
                        matriceComplete);
            }
            if(arguments.write) {
                writeMatrix(arguments.matrix_size, arguments.matrix_size,
                        matriceComplete, arguments.write);
            }
            free(matriceComplete);
        }
    }

    MPI_Finalize();
    if(a) free(a);
    return 0;
}
int main(int argc, char* argv[]) {

    int myrank;
    int nodes;
    double *a;

    int i, j, k, noTest;
    Arguments arguments;

    arguments.matrix_size = -1;
    arguments.matrice = arguments.solution = NULL;
    arguments.imprimer = 0;
    arguments.pivot = 0;
    arguments.write = NULL;

    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
    MPI_Comm_size(MPI_COMM_WORLD, &nodes);

    for(i = 1; i < argc; i++) {
        if(argv[i][0] == '-') {
            switch(argv[i][1]) {
                case 'm': if(i+1 < argc) {
                              arguments.matrix_size = atoi(argv[++i]);
                          }
                          break;
                case 'f': if(i+1 < argc) {
                              arguments.matrice = argv[++i];
                          }
                          break;
                case 's': if(i+1 < argc) {
                              arguments.solution = argv[++i];
                          }
                          break;
                case 'p': arguments.pivot = 1;
                          break;
                case 'i': arguments.imprimer = 1;
                          break; 
                case 'w': if(i+1 < argc) {
                              arguments.write = argv[++i];
                          }
                          break;
                default: fprintf(stderr, "Usage: %s -m <tailleMatrice> "\
                                 "[-f nomFichier] [-s solution]\n", argv[0]);
                         exit(EXIT_FAILURE);
            }
        }
    }

    if(arguments.matrix_size < 0) {
        fprintf(stderr,"Erreur: vous devez fournir une taille de matrice.\n");
        exit(EXIT_FAILURE);
    }

    if(arguments.matrix_size % nodes != 0) {
        fprintf(stderr, "Erreur: le nombre de noeuds doit diviser la"\
                "taille de la matrice.\n");
        exit(EXIT_FAILURE);
    }

    MPI_Barrier( MPI_COMM_WORLD );

    int nbLines = arguments.matrix_size / nodes;

    if(arguments.matrice) {
        a = readMatrixCyclic(arguments.matrice, arguments.matrix_size, 
                nbLines, myrank, nodes);
    } else {  
        a = (double *) malloc( nbLines*arguments.matrix_size*sizeof(double) );
        initializeMatrix( arguments.matrix_size, nbLines, a);
    }
    double tempsEcoule;
    MPI_Barrier( MPI_COMM_WORLD );
    tempsEcoule = -MPI_Wtime();

    factoriserLU(a, arguments.matrix_size, nbLines, MPI_COMM_WORLD, myrank,
            arguments.pivot, nodes);

    double tempsMax;
    tempsEcoule += MPI_Wtime();

    //On ramene le temps max au processeur 0
    MPI_Reduce(&tempsEcoule, &tempsMax, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);

    if(arguments.solution) {
        //on vérifie le résultat
        double *resultat = readMatrixCyclic(arguments.solution, 
                arguments.matrix_size, nbLines, myrank, nodes);
        checkResult(resultat, a, arguments.matrix_size, nbLines, MPI_COMM_WORLD);
        if(myrank == 0) {
            printf("Resultat de la factorisation lu correcte.\n");
        }

        if(resultat) free(resultat);
    }

    if (myrank == 0) {
        printf( "Factorisation LU %s pivot effectuee en %.3f msecs\n", 
                (arguments.pivot?"avec":"sans"), 1000*tempsEcoule);
    }

    if(arguments.imprimer || arguments.write) {

        //On test pour récupérer la matrice
        double *matriceComplete;
        if(myrank == 0) {
            matriceComplete = (double *) malloc(arguments.matrix_size*
                    arguments.matrix_size*sizeof(double));
        }
        for( i = 0; i < nbLines; i++) {
            MPI_Gather(&a[i*arguments.matrix_size], arguments.matrix_size, MPI_DOUBLE, 
                    &matriceComplete[i*nodes*arguments.matrix_size], 
                    arguments.matrix_size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
        }
        if( myrank == 0) {
            if(arguments.imprimer) {
                printMatrix(arguments.matrix_size, arguments.matrix_size, 
                        matriceComplete);
            }
            if(arguments.write) {
                writeMatrix(arguments.matrix_size, arguments.matrix_size, 
                        matriceComplete, arguments.write);
            }
            if(matriceComplete) free(matriceComplete);
        }


    }

#ifndef NDEBUG
    printf("rank G:%i\n",myrank);
#endif

    if(a)free(a);
    MPI_Finalize();
    return 0;
}
Ejemplo n.º 18
0
Map CreateMap(ConfigurationManager config)
{
	std::vector<unsigned char> image;
	unsigned width, height;
	unsigned int x, y;

	const char* path = config.getMapPath();
	unsigned error = lodepng::decode(image, width, height, path);
	if (error)
	{
		std::cout << "decoder error " << error << ": "
				<< lodepng_error_text(error) << std::endl;
	}

	// Get parameters
	double gridResoultion = config.getGridResolutionCM();
	double mapResoultion = config.getMapResolutionCM();
	int divider = ceil(gridResoultion/mapResoultion); // Numbers of pixels for 1 cell in matrix

	// Create map as png pixels
	char** map = initializeMatrix(height + (divider - 1) * 2,width + (divider - 1) * 2,  FREE_CELL);
	unsigned char color;
	for (y = 0; y < height; y++)
	{
		for (x = 0; x < width; x++)
		{
			if (image[y * width * 4 + x * 4 + 0]
					|| image[y * width * 4 + x * 4 + 1]
					|| image[y * width * 4 + x * 4 + 2])
			{
				// There is an obstacle in the map
				color = FREE_CELL;
			}
			else
			{
				// There is no obstacle
				color = BLOCK_CELL;
			}
			map[y + divider - 1][x + divider - 1] = color;

		}
	}

	// Create map as config cm size
	int newMapHieght = height/divider;
	int newMapWidth = width/divider;

	char** mapBlow = initializeMatrix(newMapHieght, newMapWidth, FREE_CELL);
	unsigned i,j;
	int mapBlowY = 0, mapBlowX = 0;
	bool hasObstacle = false;

	for (y = divider - 1; y < height; y += divider)
	{
		for (x = divider - 1; x < width; x += divider)
		{
			for (i = 0; i < divider && !hasObstacle; i++)
			{
				for (j = 0; j < divider && !hasObstacle; j++)
				{
					if (map[y + i][x + j] == BLOCK_CELL)
					{
						hasObstacle = true;
					}
				}
			}

			if (hasObstacle)
			{
				mapBlow[mapBlowY][mapBlowX] = BLOCK_CELL;
			}

			hasObstacle = false;
			mapBlowX++;
		}

		mapBlowX = 0;
		mapBlowY++;
	}

	// ReSize obstacle
	double* robotSize =  config.getRobotSize();
	int sizeH = ceil(((robotSize[0] / gridResoultion)- 1) / 2);
	int sizeW = ceil(((robotSize[1] / gridResoultion)- 1) / 2);

	char** newMap = initializeMatrix(newMapHieght + sizeH*2, newMapWidth + sizeW*2, FREE_CELL);
	for (int i = 0; i < newMapHieght; i++)
	{
		for (int j = 0; j < newMapWidth; j++)
		{
			if (mapBlow[i][j] == BLOCK_CELL)
			{
				for (int iNewMap = i; iNewMap <= i + sizeH + 1; iNewMap++)
				{
					for (int jNewMap = j; jNewMap <= j + sizeW + 1; jNewMap++)
					{
						newMap[iNewMap][jNewMap] = BLOCK_CELL;
					}
				}
			}
		}
	}

	// Initialize final map with wall block of size 1.
	char** finalMap = initializeMatrix(newMapHieght + 2, newMapWidth + 2, BLOCK_CELL);
	for (int i=1; i < newMapHieght + 1; i++)
	{
		for (int j=1; j< newMapWidth + 1; j++)
		{
			finalMap[i][j] = newMap[sizeH + i - 1][sizeW + j -1];
		}
	}

	printMap(map, height + 6, width + 6);
	printMap(mapBlow, newMapHieght, newMapWidth);
	printMap(newMap, newMapHieght + sizeH, newMapWidth + sizeW);
	printMap(finalMap, newMapHieght + 2, newMapWidth + 2);

	return Map(finalMap, newMapHieght + 2, newMapWidth + 2, gridResoultion / 100);
}
Ejemplo n.º 19
0
//Parallel matrix constructor
ParallelMatrix::ParallelMatrix(int rows, int columns, bool initialize) : Matrix(rows, columns) {
	if (initialize) {
		initializeMatrix();
	}
}
Ejemplo n.º 20
0
void Simulation::initialize()
{
	initializePores(pores, matrix, options);
	initializeMatrix(pores, matrix, options);
}