int main() {
	InitializeMatrix(A);
	InitializeMatrix(B);
	MultiplyMatricesParallel();
	PrintMatrix(A);
	PrintMatrix(B);
	PrintMatrix(C);
	Verify();
	return 0;
}
Beispiel #2
0
void LUmatrixSeparation(mtxMatrix ilu, int *uptr, mtxMatrix &L, mtxMatrix &U)
{
	int countL, countU;
	int i, j, s, f, k;
	double *val;
	int *col;
	countU = 0;
	for(i = 0; i < ilu.N; i++)
	{
		countU += (ilu.RowIndex[i+1] - uptr[i]);
	}
	countL = ilu.NZ + ilu.N - countU;
	InitializeMatrix(ilu.N, countL, L);
	InitializeMatrix(ilu.N, countU, U);
	k = 0;
	val = L.Value; col = L.Col;
	L.RowIndex[0] = k;
	for(i = 0; i < ilu.N; i++)
	{
		s = ilu.RowIndex[i];
		f = uptr[i];
		for(j = s; j < f; j++)
		{
			val[k] = ilu.Value[j];
			col[k] = ilu.Col[j];
			k++;
		}
		val[k] = 1.0; col[k] = i;
		k++;
		L.RowIndex[i + 1] = k;
	}
	k = 0;
	val = U.Value;
	col = U.Col;
	U.RowIndex[0] = k;
	for(i = 0; i < ilu.N; i++)
	{
		s = uptr[i];
		f = ilu.RowIndex[i + 1];
		for(j = s; j < f; j++)
		{
			val[k] = ilu.Value[j];
			col[k] = ilu.Col[j];
			k++;
		}
		U.RowIndex[i + 1] = k;
	}
}
Beispiel #3
0
//----------------------------------------------------------------------------
// Main function
int main(int argc, char* argv[]) 
{
	int i,j;
	double **Result_Seq;
	int no_thrd;
	
	if(argc != 3) 
	{
    		fprintf(stderr, "missing arguments\n", argv[0]);
    		exit(1);
  	}
	//N =3;
	//no_thrd = 2;

	N = atoi(argv[1]);
	no_thrd = atoi(argv[2]);
	
	InitializeMatrix();
	
	Result_Seq = MatMul();
	
	// row and column initialization
	row =0;
	col=0;
	MatMul_thrd(no_thrd);
	
	if(Check_result(Result_Seq))
		printf("\n----------------Results are same for both versions-------------------\n\n");
	else
		printf("\n-----#####----Error: Both result are not same-------####---------\n\n");
	
	return 0;
}
int main(int argc, char **argv)
{
	int iterations = 0;

	double startTime = 0; 
	double endTime	 = 0;
	double totalTime = 0;

	int processorsAvailable;

	// Generate the matrix.
	InitializeMatrix();

	// Initialize MPI API.
    MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &processorRank);
	MPI_Comm_size(MPI_COMM_WORLD, &processorsAvailable);

	// Ask for number of processors to be used. Loop if input is invalid.
	int processorsUsed = 1;
	while (processorsUsed != 1 && processorsUsed != 2 && processorsUsed != 4)
	{
		fflush(stdin);
		printf("How many processors should be used in the cluster? [1, 2, 4]: ");
		scanf("%d", &processorsUsed);
	}
	
	//==================================================//
	//          V V V MASTERS CODE BLOCK V V V			//
	//==================================================//
	if(processorRank == 0)
	{
		if(DEBUG)
		{
			printf("%d processors will be used.\n", processorsUsed);
			printf("\n>> Running LaPlace approximation...\n\n");
		}
		  
		// Start the timer.
		startTime = MPI_Wtime();

		// 1 processor used, the master does all the work (SEQUENTIAL).
		if(processorsUsed == 1)
		{
			// Start the timer.
			startTime = MPI_Wtime();

			iterations = SequentialApproximation();

			// If we reached to many iterations, quit.
			if(iterations > 100000)
			{
				return 0;
			}
		}

		// 2 processors used, the master and one worker.
		else if(processorsUsed == 2)
		{
			iterations = MasterBlockedApproximation(2);
		}

		// 4 processors used, the master and all three workers.
		else if(processorsUsed == 4)
		{
			iterations = MasterBlockedApproximation(4);
		}

		// Stop the timer.
		endTime = MPI_Wtime();

		printf("[SUCCESS] LaPlace approximation finished.\n");

		if(DEBUG)
		{
			printf("\n>> Printing matrix... \n\n");
			PrintMatrix();
		}

		// Output time taken.
		totalTime = (endTime - startTime);
		printf("Execution time on %2d nodes: %f\n", processorsUsed, totalTime);
	}
	//==================================================//
	//          ^ ^ ^ MASTERS CODE BLOCK ^ ^ ^ 			//
	//==================================================//
	
	//==================================================//
	//          V V V WORKER CODE BLOCK V V V			//
	//==================================================//
	else if(processorRank < processorsUsed)
	{
		// 2 processors used, the master and one worker.
		if(processorsUsed == 2)
		{
			// Worker 2 part.
			if(processorRank == 1)
			{
				WorkerBlockedApproximation();
			}
		}

		// 4 processors used, the master and all three workers.
		else if(processorsUsed == 4)
		{
			// Worker 2 part.
			if(processorRank == 1)
			{
				WorkerBlockedApproximation();
			}

			// Worker 3 part.
			else if(processorRank == 2)
			{
				WorkerBlockedApproximation();
			}

			// Worker 4 part.
			else if(processorRank == 3)
			{
				WorkerBlockedApproximation();
			}
		}
	}
	//==================================================//
	//          ^ ^ ^  WORKER CODE BLOCK ^ ^ ^ 			//
	//==================================================//

	// Finalize the MPI API, and then quit.
	MPI_Finalize();
    return 0;
}
Beispiel #5
0
int main(int argc, char *argv[]) {
    FILE *input_file, *output_file, *time_file;
    if (argc < 4) {
        fprintf(stderr, "Invalid input parameters\n");
        fprintf(stderr, "Usage: %s inputfile outputfile timefile \n", argv[0]);
        exit(1);
    }
    else {
        input_file = fopen(argv[1], "r");
        output_file = fopen(argv[2], "w");
        time_file = fopen(argv[3], "w");
        if (!input_file || !output_file || !time_file)
            exit(1);
    }

    MM_typecode matcode;
    if (mm_read_banner(input_file, &matcode) != 0) {
        printf("Could not process Matrix Market banner.\n");
        exit(1);
    }

    if (!mm_is_matrix(matcode) || !mm_is_real(matcode) || !mm_is_coordinate(matcode)) {
        printf("Sorry, this application does not support ");
        printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
        exit(1);
    }

    mtxMatrix inputMatrix, fullMatrix, LMatrix, UMatrix, UMatrixTranspose, MMatrix;
    ReadMatrix(inputMatrix, input_file);

    Timer timer;

    getRowIndex(&inputMatrix, inputMatrix.RowIndex);
    inputMatrix.RowIndex[inputMatrix.N] = inputMatrix.NZ;

	int diagNum = 0;
	for (int i = 0; i < inputMatrix.N; i++) {
        for (int j = inputMatrix.RowIndex[i]; j < inputMatrix.RowIndex[i + 1]; j++) {
            if (i == inputMatrix.Col[j]) diagNum++;
        }
    }

    if (mm_is_symmetric(matcode)) {
        InitializeMatrix(inputMatrix.N, 2 * inputMatrix.NZ - diagNum, fullMatrix);
        TriangleToFull(&inputMatrix, &fullMatrix);
        FreeMatrix(inputMatrix);
    }
    else {
        fullMatrix = inputMatrix;
    }

    int *diag = new int[fullMatrix.N];

    for (int i = 0; i < fullMatrix.N; i++) {
        for (int j = fullMatrix.RowIndex[i]; j < fullMatrix.RowIndex[i + 1]; j++) {
            if (i == fullMatrix.Col[j]) diag[i] = j;
        }
    }

//    for (int i = 0; i < fullMatrix.N + 1; i++) {
//        printf("RowIndex[%i] = %i\n", i, fullMatrix.RowIndex[i]);
//    }
//
//
//    for (int i = 0; i < fullMatrix.N; i++) {
//        printf("input[%i]= %lf\n", i, fullMatrix.Value[inputMatrix.RowIndex[i]]);
//    }
//
//    for (int i = 0; i < fullMatrix.N; i++) {
//        printf("diag[%i]= %d\n", i, diag[i]);
//    }

    timer.start();
    ilu0(fullMatrix, fullMatrix.Value, diag);
	LUmatrixSeparation(fullMatrix, diag, LMatrix, UMatrix);
	Transpose(UMatrix, UMatrixTranspose);
	Multiplicate(LMatrix, UMatrixTranspose, MMatrix);
    timer.stop();

    std::ofstream timeLog;
    timeLog.open(argv[3]);
    timeLog << timer.getElapsed();

    WriteFullMatrix(MMatrix, output_file, matcode);

    FreeMatrix(fullMatrix);

    return 0;
}