Ejemplo n.º 1
0
void task2(int size, int tid, int ctid) {
  {
    std::stringstream ss;
    ss << "task 2 with tid " << tid << " and ctid " << ctid << " started" << std::endl;
    std::cout << ss.str();
  }

  matrix mk, mn;

  mk = generateMatrix(size);
  mn = generateMatrix(size);

  int result = func2(mk, mn);

  if (size < 8) {
    std::stringstream ss;
    ss << "task 2 result: " << result;
    ss << std::endl;
    std::cout << ss.str();
  }

  {
    std::stringstream ss;
    ss << "task 2 with tid " << tid << " and ctid " << ctid << " finished" << std::endl;
    std::cout << ss.str();
  }
}
Ejemplo n.º 2
0
void task3(int size, int tid, int ctid) {
  {
    std::stringstream ss;
    ss << "task 3 with tid " << tid << " and ctid " << ctid << " started" << std::endl;
    std::cout << ss.str();
  }

  vector w, x;
  matrix ms, mz;

  w = generateVector(size);
  x = generateVector(size);
  ms = generateMatrix(size);
  mz = generateMatrix(size);

  vector result = func3(ms, mz, w, x);

  if (size < 8) {
    std::stringstream ss;
    ss << "task 3 result: ";
    ss << '[';
    for (int i = 0; i < result.size(); ++i) {
      ss << result[i] << " ";
    }
    ss << ']';
    ss << std::endl;
    std::cout << ss.str();
  }

  {
    std::stringstream ss;
    ss << "task 3 with tid " << tid << " and ctid " << ctid << " finished" << std::endl;
    std::cout << ss.str();
  }
}
Ejemplo n.º 3
0
void testMatrixMulti(void)
{
	int **matrixA, **matrixB;
	int	**matrixResult;
	int	rowNumOfA, colNumOfA;
	int	rowNumOfB, colNumOfB;
	int rowNumOfR, colNumOfR;

	rowNumOfA = 2;
	colNumOfA = 3;

	rowNumOfB = 3;
	colNumOfB = 2;

	matrixA = generateMatrix(rowNumOfA, colNumOfA);
	matrixB = generateMatrix(rowNumOfB, colNumOfB);

	matrixResult = make2DArray(rowNumOfA, colNumOfA);

	printf("----------- test: matrix multiplication ------------------\n");
	if(!matrixMulti(matrixResult, matrixA, rowNumOfA, colNumOfA, matrixB, rowNumOfB, colNumOfB)) {
		printf("matrixA\n");
		printMatrix(matrixA, rowNumOfA, colNumOfA);

		printf("matrixB\n");
		printMatrix(matrixB, rowNumOfB, colNumOfB);

		printf("matrixResult = matrixA x matrixB\n");
		rowNumOfR = rowNumOfA;
		colNumOfR = colNumOfB;
		printMatrix(matrixResult, rowNumOfR, colNumOfR);	
	}
	else
		printf("Fail to matrix multiplication\n");
}
Ejemplo n.º 4
0
void task1(int size, int tid, int ctid) {
  {
    std::stringstream ss;
    ss << "task 1 with tid " << tid << " and ctid " << ctid << " started" << std::endl;
    std::cout << ss.str();
  }

  vector a, b;
  matrix ma, md;

  a = generateVector(size);
  b = generateVector(size);
  ma = generateMatrix(size);
  md = generateMatrix(size);

  vector result = func1(a, b, ma, md);

  if (size < 8) {
    std::stringstream ss;
    ss << "task 1 result: ";
    ss << '[';
    for (int i = 0; i < result.size(); ++i) {
      ss << result[i] << " ";
    }
    ss << ']';
    ss << std::endl;
    std::cout << ss.str();
  }
  
  {
    std::stringstream ss;
    ss << "task 1 with tid " << tid << " and ctid " << ctid << " finished" << std::endl;
    std::cout << ss.str();
  }
}
Ejemplo n.º 5
0
int main() {
    int i, args[THREADS_COUNT];
    int size = MATRIX_SIZE / THREADS_COUNT;
    pthread_t thID[THREADS_COUNT];
    allocateMemory(&matrix1);
    allocateMemory(&matrix2);
    allocateMemory(&result);
    generateMatrix(matrix1);
    sleep(1);
    generateMatrix(matrix2);
    for (i = 0; i < THREADS_COUNT; i++) {
        args[i] = i;
        pthread_create(&(thID[i]), NULL, multMatrix, (void*)(&(args[i])));
    }
    for (i = 0; i < THREADS_COUNT; i++) {
        pthread_join(thID[i], NULL);
    }

/*  printMatrix(matrix1);
    printf("\n");
    printMatrix(matrix2);
    printf("\n");
    printMatrix(result); */
    freeMemory(matrix1);
    freeMemory(matrix2);
    freeMemory(result);
    return 0;
}
Ejemplo n.º 6
0
void testMatrixSum(void)
{
	int **matrixA, **matrixB;
	int	**matrixResult;
	int	rowNum, colNum;

	rowNum = colNum = 3;

	matrixA = generateMatrix(rowNum, colNum);
	matrixB = generateMatrix(rowNum, colNum);

	matrixResult = make2DArray(rowNum, colNum);

	matrixSum(matrixResult, matrixA, matrixB, rowNum, colNum);

	printf("----------- test: matrix addition ------------------\n");

	printf("matrixA\n");
	printMatrix(matrixA, rowNum, colNum);

	printf("matrixB\n");
	printMatrix(matrixB, rowNum, colNum);

	printf("matrixResult = matrixA + matrixB\n");
	printMatrix(matrixResult, rowNum, colNum);

}
Ejemplo n.º 7
0
int main(int argc, char *argv[])
{
    int n;
    float **m = NULL;
    float **k = NULL;
    float **kt = NULL;
    struct timeval fin, ini;
    float **res = NULL;
    int n_threads = 1;

    if ( argc != 3 )
    {
        printf("Error: ./%s <matrix size> <threads>\n", argv[0]);
        return -1;
    }

    n_threads = atoi(argv[2]);
    n = atoi(argv[1]);
    m = generateMatrix(n);
    k = generateMatrix(n);
    kt = generateEmptyMatrix(n);
    res = generateEmptyMatrix(n);

    if ( !m || !k || !res || !kt)
    {
        return -1;
    }

    omp_set_num_threads(n_threads);

    gettimeofday(&ini, NULL);

    /* Main computation */
    compute(m, k, kt, res, n);
    /* End of computation */

    gettimeofday(&fin, NULL);
#ifdef OUTPUT_RESULT
    int i, j;
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < n; j++)
            printf("%.1f ", res[i][j]);
        printf("\n");
    }
#else
    printf("%f", ((fin.tv_sec * 1000000 + fin.tv_usec) - (ini.tv_sec * 1000000 + ini.tv_usec)) * 1.0 / 1000000.0);
#endif
    free(m);
    return 0;
}
Ejemplo n.º 8
0
int main(int argc, char* argv[]){
	int tam=0;
	struct timeval t_ini, t_fin;
	num **A=NULL, **B=NULL, **e_matrix=NULL, **t_matrix=NULL, **B_t=NULL;
	int i=0, j=0;
	tam=atoi(argv[1]);
	
	/*Generar las matrices*/
	A=generateMatrix(tam);
	B=generateMatrix(tam);
	e_matrix=generateEmptyMatrix(tam);
	t_matrix=generateEmptyMatrix(tam);
	B_t=generateEmptyMatrix(tam);
	
	/*Multiplicar matrices*/
		/* Multiplicacion normal*/
	gettimeofday(&t_ini,NULL);
	mult(A, B, e_matrix,tam);
	gettimeofday(&t_fin,NULL);
	printf("%f\n", ((t_fin.tv_sec*1000000+t_fin.tv_usec)-(t_ini.tv_sec*1000000+t_ini.tv_usec))*1.0/1000000.0);
	
		/*Multiplicacion traspuesta*/	
	traspuesta(B, B_t, tam);
	gettimeofday(&t_ini, NULL);
	mult_t(A, B_t, t_matrix,tam);
	gettimeofday(&t_fin, NULL);
	printf("%f\n", ((t_fin.tv_sec*1000000+t_fin.tv_usec)-(t_ini.tv_sec*1000000+t_ini.tv_usec))*1.0/1000000.0);
	
	
	/*printf("Normal\n");
	for(i=0; i<tam; i++){
		for(j=0; j<tam; j++){
			printf("| %f |", B[i][j]);
		}
		printf("\n");
	}
	printf("\nTraspueta\n");
	for(i=0; i<tam; i++){
		for(j=0; j<tam; j++){
			printf("| %f |", B_t[i][j]);
		}
		printf("\n");
	}*/
	freeMatrix(A);
	freeMatrix(B);
	freeMatrix(B_t);
	freeMatrix(e_matrix);
	freeMatrix(t_matrix);
	return 0;
} 
Ejemplo n.º 9
0
int main(int argc, char** argv)
{
	float **A=NULL, **B=NULL, **C=NULL;
	int i=0,j=0,k=0;
	struct timeval fin,ini;
	unsigned long long num;
	unsigned long thr;
	int sum=0; 
	thr=atoi(argv[1]);
	num=atoi(argv[2]);


	A = generateMatrix(num);
	B = generateMatrix(num);
	C = generateEmptyMatrix(num);
	if ( !A || !B || !C)
	{
		printf("Error when allocationg matrix\n");
		freeMatrix(A);
		freeMatrix(B);
		freeMatrix(C);
		return -1;
	}
	
	gettimeofday(&ini,NULL);
	
	omp_set_num_threads(thr);
	/* Bloque de computo */
	#pragma omp parallel for private(k,i,j) firstprivate(A,B) shared(C) schedule(static)
	for (i=0; i<num; i++){
		for (j=0; j<num; j++){
			for (k=0; k<num; k++){
				C[i][j]+= A[i][k] * B[k][j];
			}		
		}
	}

	/* Fin del computo */
	gettimeofday(&fin,NULL);

	//printf("Resultado: %f\t%f\t%f\n",C[0][0],C[1][1],C[2][2]);
	printf("Tiempo: %f\n", ((fin.tv_sec*1000000+fin.tv_usec)-(ini.tv_sec*1000000+ini.tv_usec))*1.0/1000000.0);

	freeMatrix(A);
	freeMatrix(B);
	freeMatrix(C);

	return 0;
}
Ejemplo n.º 10
0
int main(int argc, char * argv[]){
		int n = 5, i;
		int **edge = generateMatrix(n);
		printMatrix(edge, n);
		int **result = floyd(n, edge);


}
Ejemplo n.º 11
0
int main() {
    generateMatrix();    
    std::vector<int> colSum(MatrixSize);
    for(int i = 0; (i < 100); i++) {
        columnSum2(colSum);
    }
    std::cout << "colSum[10] = " << colSum[10] << std::endl;
    return 0;
}
Ejemplo n.º 12
0
int main(){
    vector<vector<int> > res = generateMatrix(5);
    for(int i=0; i<res.size(); i++){
        for(int j=0; j<res[i].size(); j++)
            printf("%d ", res[i][j]);
        printf("\n");
    }
    return 0;
}
Ejemplo n.º 13
0
int main(int argc, char* argv[]) {

  /* Set parameters */
  int N = atoi(argv[1]);
  int nIterations = atoi(argv[2]);


  /* Start parallel calculations */
  int rank, p;
  MPI_Init(&argc, & argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &p);


  /* p has to be a divisor of N !! */
  if(rank==0) assert(!(N%p));


  /* Test the used algorithms */
  //matrix_testAll();


  /* Generate the matrix */
  double matrix[N*N/p];
  generateMatrix(matrix,N);


  /* Run the powerMethod algorithm */
  double start = MPI_Wtime();
  double lambda = powerMethod(matrix,nIterations,N);
  double stop = MPI_Wtime();



  /* Calculating times */
  double timediff = stop - start;
  double times[p];
  double average = 0;
  MPI_Gather(&timediff,1,MPI_DOUBLE,times,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
  if(rank==0){
    for(int i=0; i<p; i++)
      average += times[i];
    average /= p;
  }


  /* Print the results */
  if(rank==0)
    printf("dimension\t%d\tlambda\t%f\ttime\t%f\n",N,lambda,average);


  /* End MPI */
  MPI_Finalize();

  return 0;
}
int main(){
    int i,n;

    while(scanf("%d", &n) && n != 0) {
        generateMatrix(n);
        printf("\n");
    }

    return 0;
}
Ejemplo n.º 15
0
int main()
{
	int n;
	while (~scanf("%d", &n)) {
		int **m = generateMatrix(n);
		printf("The spiral matrix of size %d is:\n", n);
		print_matrix(m, n);
		free_matrix(m, n);
	}
}
int main (int argc, char ** argv)
{
	if (argc != 3) 
	{
		printf("Use: %s <N> <T>,  onde 'N' é a dimensão da matriz unitária e 'T' é o número de threads.\n", argv[0]);
		return 1;
	}

	//Retira valores dos argumentos passados pela linha de comando
	matrix_size = atoi(argv[1]);
	number_of_threads = atoi(argv[2]);

	//Aloca espaço para variáveis e ponteiros para colunas
	A = allocateMatrix(matrix_size);

	//Gera matrizes A e B
	generateMatrix(A, matrix_size);

	//Imprime A e B (teste)
	if (matrix_size < 20)
		printMatrix(A, matrix_size, "A");

	//Ponteiro com threads
	pthread_t * p_threads;
	//Alocação de espaço para threads de acordo com o número de threads
	p_threads = (pthread_t *) malloc(number_of_threads * sizeof(pthread_t));


	//Inicializa o mutex
  	pthread_mutex_init(&mut, NULL);

	//Captura tempo inicial
	clock_gettime(CLOCK_MONOTONIC, &initial_time);

	//Criação de threads
	createThreads(p_threads, number_of_threads, matrix_size, A);

	//Join das threads
	joinThreads(p_threads);

	//Captura tempo final
	clock_gettime(CLOCK_MONOTONIC, &end_time);
  	
  	//imprime resultado
	printf("A soma de todos os termos é: %d\n", sum);

	//Calcula tempo final
	double elapsed = end_time.tv_sec - initial_time.tv_sec;
	elapsed += (end_time.tv_nsec - initial_time.tv_nsec) / 1000000000.0;
 	printf ("O tempo de processamento foi de: %f segundos\n", elapsed);   
	
	//Inicializa o mutex
  	pthread_mutex_destroy(&mut);  
	return 0;
}
Ejemplo n.º 17
0
int main(int argc, char *argv[]){
	// Instantiate tallied variables.
	int i, j, width, correct, hintCount, guesses, hints, trys;
	//Instantiate stats array
	int stats[5]  = {0};
	// Determine width of Matrix	
	width = getWidth(argc, argv);	
	// Generate matrix based on width
	complex** M = generateMatrix(width);	
	// Create randmom number generator.
	srand((unsigned)time(NULL));	
	// Create string array for user input.
	char choice[20];	
	//	Create array of type complex to store generated row column pairs
	complex uniques[width * width];	
	// Continue game until user quits.
	while(strncmp(choice, "Q", 1) != 0){
		// Generate random row column pairs for ser to guess.
		i = rand()  % width;
		j = rand()  % width;
		// Reset counting variable for each generated row column pair.
		correct = hints = guesses = 0;
		while(correct == 0 && strncmp(choice, "Q", 1) != 0){
			// Count number of hints used for this specific pair.
			hintCount = 0;
			//	Display message to user displaying options.
			printf("M[0][0]=%p. M[i][j]=%p What's i and j?\n(Q to Quit or H or
			 HH or HHH for hints.): ", &M[0][0], &M[i][j]);
			// Recieve and store user input.
			fgets(choice, 20, stdin);
			// Check if user requested hints
			hintCount += checkHints(choice, M, width);
			// If user did not want hints check is valid answer submitted
			if (hintCount == 0){
				//	Check answer
				correct = checkAnswer(choice, i, j);
			}
			//	Accumulate hints used.
			hints += hintCount;
			// Accumulate guesses used.
			guesses++;
		}
		//	If user has not quit calculate stats for pair.
		if (strncmp(choice, "Q", 1) != 0){
			setUnique(uniques, i, j, stats);
			setStats(stats,hints, guesses);
		}
	}
	// Print stats of the game after user has quit.
	printStats(stats);
	// terminate matrix.
	killMatrix(M, width);
}
Ejemplo n.º 18
0
int main(int argc, char *argv[])
{
  int rank, size;
  double start_time,end_time;
  MPI_Init(&argc,&argv);

  MPI_Comm_size(MPI_COMM_WORLD, &size); // p
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  MPI_Barrier(MPI_COMM_WORLD);
  int retval = generateMatrix(size, rank);    
  if (retval != 0) {
    MPI_Finalize();
    return retval;  
  }
  MPI_Barrier(MPI_COMM_WORLD);
  // start timing
  if (MASTER(rank)) {
    start_time = MPI_Wtime();
  }
  double spectralRadius = powerMethod(rank);
  if (MASTER(rank)) {
    end_time = MPI_Wtime();
  }
  // end timing

  if (MASTER(rank)) {
    printf("The spectral radius is %f\n", spectralRadius);
    printf("It took %f seconds\n", end_time-start_time);

    /*
    int index = 0;
    for (index = 0; index < n; index++) {
      printf("%f ", x[index]);
    }
    printf("\nsize of n is %d\n", n);
    */
    // checking
    if(cs240_verify(x,n,end_time-start_time)>0){
        printf("yay, we win!\n");
    }else{
        printf("Boo, we lose... again\n");
    }
  }


  //printf("calling MPI_Finalize()\n");
  MPI_Finalize();
  return(0);
}
Ejemplo n.º 19
0
matrix operator*(matrix left, matrix right) {
	assert(left.size() > 0);
	assert(right.size() > 0);
	assert(left[0].size() == right.size());

	matrix result = generateMatrix(left.size(), right[0].size(), 0);
	for (int i = 0; i < left.size(); ++i) {
		for (int j = 0; j < right[0].size(); ++j) {
			for (int k = 0; k < left[0].size(); ++k) {
				result[i][j] += left[i][k] * right[k][j];
			}
		}
	}
	return result;
}
Ejemplo n.º 20
0
int main(int arg, char *argv[]) {
    // insert code here...
    printf("LeetCode 059 Spiral Matrix II, C ...\n\n");

    int n = 3;
    int **m = generateMatrix(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", m[i][j]);
        }
        printf("\n");
    }

    return 0;
}
Ejemplo n.º 21
0
int main() {
    int n = 3;
    int i, j;
    int**matrix = NULL;

    matrix = generateMatrix(n);

    for(i = 0; i<=n-1; i++) {
    	for(j = 0; j<=n-1; j++){
    		printf("%d ", matrix[i][j]);
    	}
    	printf("\n");
    }
    return 0;
}
Ejemplo n.º 22
0
int _tmain(int argc, _TCHAR* argv[])
{
	int** matrix = generateMatrix(rows, cols);
	printOutMatrix(matrix, rows, cols);
	std::vector<int> tmpPath;

	dfsMinSumPath(matrix, 0, tmpPath, 0);
	std::cout << "The min path sum is: " << minPathSum << std::endl;
	std::cout << "The min sum path is: ";
	for (int i = 0; i < minSumPath.size(); ++i)
		std:: cout << minSumPath[i] << ", ";

	std::cout << std::endl << "Total func calls: " << totalFuncCalls << std::endl;
	std::cout << std::endl;

	return 0;
}
Ejemplo n.º 23
0
void testMatrixTranspose(void)
{
	int **matrixA;
	int	**matrixAT;
	int	rowNumOfA, colNumOfA;
	int rowNumOfAT, colNumOfAT;

	rowNumOfA = 2;
	colNumOfA = 3;
	matrixA = generateMatrix(rowNumOfA, colNumOfA);

	rowNumOfAT = colNumOfA;
	colNumOfAT = rowNumOfA;
	matrixAT = make2DArray(rowNumOfAT, colNumOfAT);

	printf("----------- test: matrix transpose ------------------\n");
	matrixTranspose(matrixAT, matrixA, rowNumOfA, colNumOfA);

	printf("matrixA\n");
	printMatrix(matrixA, rowNumOfA, colNumOfA);

	printf("matrixAT\n");
	printMatrix(matrixAT, rowNumOfAT, colNumOfAT);		
}
Ejemplo n.º 24
0
int main() {
    print(generateMatrix(4),4);
}
Ejemplo n.º 25
0
int main( int argc, char **argv ) {
  MPI_Init( &argc, &argv );
  int rank, P;
  MPI_Comm_rank( MPI_COMM_WORLD, &rank );
  MPI_Comm_size( MPI_COMM_WORLD, &P );

  int s = read_int( argc, argv, "-s", 5 );
  int d = read_int( argc, argv, "-d", 1 );
  int ds = read_int( argc, argv, "-ds", 0 );
  int_d n = ((int_d) 1) << s;
  int_d colsPerProc = n/P;
  n = colsPerProc * P;
  double density = 1.*d/n/(1 << ds);

  if( rank == 0 )
    printf("Using n=%ld, d=%d on P=%d processors\n", n, d, P );

  int sqrtP = (int) sqrt(1.*P);
  if( sqrtP*sqrtP != P ) {
    if( rank == 0 )
      printf("Requires a square processor grid\n");
    MPI_Finalize();
    exit(-1);
  }

  // construct row and column communicators
  MPI_Group initialGroup;
  MPI_Comm_group( MPI_COMM_WORLD, &initialGroup );
  MPI_Comm rowComm;
  MPI_Comm colComm;
  MPI_Group gp;
  MPI_Comm cm;
  for( int i = 0; i < sqrtP; i++ ) {
    int rranks[sqrtP];
    int cranks[sqrtP];
    bool rc = false, cc = false;
    for( int j = 0; j < sqrtP; j++ ) {
      rranks[j] = sqrtP*i+j;
      cranks[j] = sqrtP*j+i;
      if( rranks[j] == rank )
	rc = true;
      if( cranks[j] == rank )
	cc = true;
    }
    MPI_Group_incl( initialGroup, sqrtP, rranks, &gp );
    MPI_Comm_create( MPI_COMM_WORLD, gp, &cm );
    if( rc )
      rowComm = cm;
    MPI_Group_free( &gp );
    MPI_Group_incl( initialGroup, sqrtP, cranks, &gp );
    MPI_Comm_create( MPI_COMM_WORLD, gp, &cm );
    if( cc )
      colComm = cm;
    MPI_Group_free( &gp );
  }
  MPI_Group_free( &initialGroup );

  int rrank, crank;
  MPI_Comm_rank( rowComm, &rrank );
  MPI_Comm_rank( colComm, &crank );
  int_d bs = n/sqrtP;

  BlockIndexColMajor ai = BlockIndexColMajor(crank*bs, rrank*bs, bs, bs );
  BlockIndexRowMajor bi = BlockIndexRowMajor(crank*bs, rrank*bs, bs, bs );

  //printf("%d Generating matrices\n", rank);
  Matrix *A = generateMatrix( &ai, density, time(0)+100*rank );
  Matrix *B = generateMatrix( &bi, density, time(0)+100*rank+1 );
  //Matrix *A = generateMatrix( &ai, density, 2*rank+39 );
  //Matrix *B = generateMatrix( &bi, density, 2*rank+1+39 );

  /*
  printf("A %d: ", rank);
  for( auto it = A->begin(); it != A->end(); it++ )
    printf("(%d %d) ", it->first.first, it->first.second);
  printf("\n");

  printf("B %d: ", rank);
  for( auto it = B->begin(); it != B->end(); it++ )
    printf("(%d %d) ", it->first.first, it->first.second);
  printf("\n");
  */

  //printf("%d collecting input\n", rank );
  Matrix *fullA = gather(A, rank, P );
  Matrix *fullB = gather(B, rank, P );

  //printf("%d dist multiply\n", rank );
  Matrix *C = spSUMMA( A, B, n, sqrtP, rowComm, rrank, colComm, crank );

  /*
  printf("C %d: ", rank);
  for( auto it = C->begin(); it != C->end(); it++ )
    printf("(%d %d) ", it->first.first, it->first.second);
  printf("\n");
  */

  //printf("%d collecting output\n", rank );
  Matrix *fullC = gather(C, rank, P );

  /*
  printf("fC %d: ", rank);
  for( auto it = fullC->begin(); it != fullC->end(); it++ )
    printf("(%d %d) ", it->first.first, it->first.second);
  printf("\n");
  */

  //printf("%d checking answer\n", rank );
  if( rank == 0 ) {
    double maxError = 0.;
    std::sort( fullA->begin(), fullA->end(), CompColMajorEntry );
    std::sort( fullB->begin(), fullB->end(), CompRowMajorEntry );
    Matrix *testC = sortDedup( local_multiply( fullA, fullB ), CompColMajorEntry );
    printf("sizes %lu vs %lu\n", testC->size(), fullC->size());
    std::sort( fullC->begin(), fullC->end(), CompColMajorEntry );
    for( auto it1 = testC->begin(), it2 = fullC->begin();
	 it1 != testC->end() && it2 != fullC->end();
	 it1++, it2++ ) {
      if( it1->first.first != it2->first.first ||
	  it1->first.second != it2->first.second )
	printf("Error: %ld %ld vs %ld %ld\n", it1->first.first, it1->first.second, it2->first.first, it2->first.second );
      else {
	if( it2->second-it1->second > maxError )
	  maxError = it2->second-it1->second;
	if( it1->second-it2->second > maxError )
	  maxError = it1->second-it2->second;
      }
    }
    printf("max error %e\n", maxError );
  }
  MPI_Finalize();
}
Ejemplo n.º 26
0
int main(int argc, char* argv[])
{
	int x;

	printf("\n\nWähle einen Modus:\n1 = neue Matrix soll generiert werden\n2 = zwei Matrixen sollen addiert werden\n3 = zwei Matrixen sollen multipliziert werden\n");
	scanf("%d",&x);

	if(x == 1)
	{
		int n, m;
		char fileName[100];
		printf("\n\n Wie groß soll die generierte Matrix sein?\n Gebe eine Zeilenanzal (n) ein:\n");
		scanf("%d",&n);
		printf("\nGebe eine Spaltenanzal (m) ein:\n");
		scanf("%d",&m);
		printf("\nGebe ein Dateiname ein, in die die Matrix gespeichert werden soll:\n");
		scanf("%s",fileName);

		struct matrix *newMatrix = allocMatrix(n, m);
		generateMatrix(newMatrix, 100);
		printf("\n\n Generierte Matrix:\n");
		printMatrix(newMatrix);
		writeFile(newMatrix, fileName);
		freeMatrix(newMatrix);
	}
	else if(x == 2 || x == 3)
	{
		char fileName1[100];
		char fileName2[100];
		char fileName3[100];
		printf("\n\nGebe ein Dateiname ein, für die erste Matrix der Berechnung:\n");
		scanf("%s",fileName1);
		printf("\n\nGebe ein Dateiname ein, für die zweite Matrix der Berechnung:\n");
		scanf("%s",fileName2);
		printf("\n\nGebe ein Dateiname ein, in der die Ergebnismatrix gespeichert werden soll:\n");
		scanf("%s",fileName3);

		struct matrix *matrixA = einlesen(fileName1);
		struct matrix *matrixB = einlesen(fileName2);
		struct matrix *matrixResult;

		if(x == 2)
		{
			matrixResult = add(matrixA, matrixB);
		}
		else
		{
			matrixResult = mult(matrixA, matrixB);
		}	
	
		writeFile(matrixResult, fileName3);
		freeMatrix(matrixA);
		freeMatrix(matrixB);
		freeMatrix(matrixResult);
	}
	else
	{
		printf("falscher Modus\n");
	}

return 0;
}
Ejemplo n.º 27
0
matrix generateMatrix(int size, int filler) {
	return generateMatrix(size, size, filler);
}
int main() {
    int n = 4;
    int **ans = generateMatrix(n);
    printMatrix(ans, n, n);
    return 0;
}
Ejemplo n.º 29
0
Archivo: PC.c Proyecto: blaunay/preesm
    DWORD WINAPI computationThread_PC( LPVOID lpParam ){
        // Buffer declarations
        long i ;

        {
            init_ProdMatVect_0_prodScalVect_init_accIn(vectorOut_accIn);
        }

        for(;;){
            vectorOut_accIn_2 = &vectorOut_accIn[0];
            generateMatrix(outMat_in, 9/*size*/);
            generateVect(outVect_in, 3/*size*/);
            {//ProdMatVect_0_explode_vectorIn
                out_ScalIn = &outVect_in[0];
                out_ScalIn_1 = &outVect_in[3];
                out_ScalIn_0 = &outVect_in[2];
            }
            {//ProdMatVect_0_explode_matrixIn
                out_vector1In = &outMat_in[0];
                out_vector1In_0 = &outMat_in[9];
                out_vector1In_1 = &outMat_in[6];
            }
            {//ProdMatVect_0_prodScalVect
                init_inLoopPort_0(outLoopPort_0_inLo_0, 1/*init_size*/);
                for(i = 0; i<3 ; i ++)
                {//cluster_0
                    char *inSub_i_cluster_0__0 = &cluster_0_in_in [((i*(1))%3)];
                    int *outSub_i_out_vecto_0 = &out_vector1In [((i*(1))%0)];
                    char *outSub_i_vectorOut_0 = &vectorOut_accIn [((i*(1))%3)];
                    int *outSub_i_out_ScalIn = &out_ScalIn [((i*(1))%0)];
                    mux();
                    {//productScal
                        {//brScal
                            memcpy(outLoopPort_0_inLo_0, out_scal2, 1*sizeof(char)/*size*/);
                        }
                        mult(outSub_i_out_vecto_0, res_op1);
                        add(res_op1, outSub_i_vectorOut_0, inSub_i_cluster_0__0);
                    }
                }
                for(i = 0; i<3 ; i ++)
                {//brScal
                    char *inSub_i_vectorOut__0 = &vectorOut_accIn_0 [((i*(1))%3)];
                    char *inSub_i_vectorFina_0 = &vectorFinal_in [((i*(1))%3)];
                    char *outSub_i_cluster_0_0 = &cluster_0_in_in [((i*(1))%3)];
                    brScal();
                }
            }
            {//ProdMatVect_0_prodScalVect_1
                init_inLoopPort_0(outLoopPort_0_inLo_0, 1/*init_size*/);
                for(i = 0; i<3 ; i ++)
                {//cluster_0
                    char *inSub_i_cluster_0__0 = &cluster_0_in_in [((i*(1))%3)];
                    int *outSub_i_out_vecto_0 = &out_vector1In_0 [((i*(1))%0)];
                    char *outSub_i_vectorOut_0 = &vectorOut_accIn_0 [((i*(1))%3)];
                    int *outSub_i_out_ScalI_0 = &out_ScalIn_1 [((i*(1))%0)];
                    mux();
                    {//productScal
                        {//brScal
                            memcpy(outLoopPort_0_inLo_0, out_scal2, 1*sizeof(char)/*size*/);
                        }
                        mult(outSub_i_out_vecto_0, res_op1);
                        add(res_op1, outSub_i_vectorOut_0, inSub_i_cluster_0__0);
                    }
                }
                for(i = 0; i<3 ; i ++)
                {//brScal
                    char *inSub_i_vectorOut__0 = &vectorOut_accIn_1 [((i*(1))%3)];
                    char *inSub_i_vectorFina_0 = &vectorFinal_in_0 [((i*(1))%9)];
                    char *outSub_i_cluster_0_0 = &cluster_0_in_in [((i*(1))%3)];
                    brScal();
                }
            }
            {//ProdMatVect_0_prodScalVect_2
                init_inLoopPort_0(outLoopPort_0_inLo_0, 1/*init_size*/);
                for(i = 0; i<3 ; i ++)
                {//cluster_0
                    char *inSub_i_cluster_0__0 = &cluster_0_in_in [((i*(1))%3)];
                    int *outSub_i_out_vecto_0 = &out_vector1In_1 [((i*(1))%0)];
                    char *outSub_i_vectorOut_0 = &vectorOut_accIn_1 [((i*(1))%3)];
                    int *outSub_i_out_ScalI_0 = &out_ScalIn_0 [((i*(1))%0)];
                    mux();
                    {//productScal
                        {//brScal
                            memcpy(outLoopPort_0_inLo_0, out_scal2, 1*sizeof(char)/*size*/);
                        }
                        mult(outSub_i_out_vecto_0, res_op1);
                        add(res_op1, outSub_i_vectorOut_0, inSub_i_cluster_0__0);
                    }
                }
                for(i = 0; i<3 ; i ++)
                {//brScal
                    char *inSub_i_vectorOut__0 = &vectorOut_accIn_2 [((i*(1))%0)];
                    char *inSub_i_vectorFina_0 = &vectorFinal_in_1 [((i*(1))%3)];
                    char *outSub_i_cluster_0_0 = &cluster_0_in_in [((i*(1))%3)];
                    brScal();
                }
            }
            {//ProdMatVect_0_roundBuffer_vectorOut
                memcpy(out_inResult, vectorFinal_in_1, 3*sizeof(char)/*size*/);
            }
            display(out_inResult, 3/*size*/);
        }

        return 0;
    }//computationThread
Ejemplo n.º 30
0
/**
 * Sample arguments
 *
 * To transpose a randomly generated 64x32 matrix using basic algorithm 
 * (both input and output displayed on screen)
 *              ./transposeOP -basic -m 64 -n 32
 *
 *
 */
int main(int argc, char *argv[])
{
    int *a, *b;
    const char *usage = "Usage: basictransposeOP [[-i <infile>] -m <row> -n <column>] [-o <outfile>] [-noIO] [-noinit]\n";
    char *infile=NULL, *outfile=NULL;
    int row=0, column=0, tile1size=0, tile2size=0;
    int i, noio=0, noinit=0;;
    
    
    if (argc <=1 ) {
        printf("%s", usage);
        exit(0);
    }
    //    printf("%d\n", argc);
    for (i=1; i<argc; i++) {
        //        printf("%s", argv[i]);
        if (!strcmp("-i", argv[i])) {
            infile = argv[++i];
        } else if (!strcmp("-o", argv[i])) {
            outfile = argv[++i];
        } else if (!strcmp("-m", argv[i])) {
            sscanf(argv[++i], "%d", &row);
        } else if (!strcmp("-n", argv[i])) {
            sscanf(argv[++i], "%d", &column);
        } else if (!strcmp("-noIO", argv[i]) || !strcmp("-noio", argv[i])) {
            noio=1;
        } else if (!strcmp("-noinit", argv[i])) {
            noinit=1;
        }
    }
    
    if (infile == NULL) {
        // no input file provided
        if (row==0) {
            row=ROW;
        }
        if (column==0) {
            column=COLUMN;
        }
        a = generateMatrix(row, column, noinit);

    } else {
        if (row<=0 || column<=0) {
            printf("Please provide a valid dimension of the input matrix.\n");
            printf("%s", usage);
            exit(0);
        }
        a = loadMatrix(infile, row, column);
    }
    b = allocateMatrix(column, row);
    
    if (!noio) {
        printf("\nBefore Transpose: \n");
        printm(a, row, column);
    }
    
    printf("Starting transpose...\n");

    transpose(a, b, row, column, row, column);
    printf("Transpose complete.\n");
    if (!noio) {
        printf("\nAfter Transpose: \n");
        if (outfile==NULL) {
            printm(b, column, row);
        } else {
            printmf(b, column, row, outfile);
        }
    }
    
    free(a);
    free(b);
    return 1;
}