Beispiel #1
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;
}
Beispiel #2
0
int main(int argc, char** argv)
{
	FILE *out;
	coo_t *ilu = NULL, *A = NULL;

	double *b = NULL;
	ilu = (coo_t*) malloc (sizeof(coo_t));
	A = (coo_t*) malloc (sizeof(coo_t));
	
	int device = 0; //0 - CPU, 1 - GPU
	int key_ilu = 0, key_A = 0, key_rhs = 0, dimb = 1, part = 1;
	
	if (argc == 1)
	{
		
		fprintf(stderr,"Usage: %s -A [martix-market-filename] -ilu [martix-market-filename] -rhs [martix-market-filename]\n",argv[0]);
		fprintf(stderr,"Optional: -d compute on GPU, -h compute on CPU(default), -b dim (where dim - number of phases on hydrodynamic model, default dim = 1)\n");
			exit(1);
	}
	else 
	    for (int i = 1; i < argc ; i++)
	    {
			if (!strcmp(argv[i],"-ilu"))
			{
			    key_ilu = 1;
			    if( read_coo_MM(argv[i+1], ilu) != 0)
					exit(1);
			}
			if (!strcmp(argv[i],"-A"))
			{
				key_A = 1;
				if( read_coo_MM(argv[i+1], A) != 0)
					exit(1);
			}
			if (!strcmp(argv[i],"-rhs"))
			{
				key_rhs = 1;
				if( read_coo_vector(argv[i+1], &b) != 0)
				exit (1);
			}
			if (!strcmp(argv[i],"-d"))
				device = 1;

			if (!strcmp(argv[i],"-b"))
				dimb = atoi(argv[i+1]);
		
			if (!strcmp(argv[i],"-p"))
			    part = atoi(argv[i+1]);
	    }
	
	if ( (key_A) && (key_rhs) )
	{
	    if (key_ilu == 0)
	    { 
			ilu = allocCOO (A->nnz);
			if( copyCOO (ilu,A) != 0) exit (1);
	    }
	    printf ("Load Matrix complete\n"); /*0-index based*/
	}
	else 
	{
	    printf ("Load Matrix Error\n");
	    exit(1);
	}

	
	csr_t* A_mat = allocCSR(A->nnz,A->n);
	csr_t* ilu_mat = allocCSR(ilu->nnz,ilu->n);
	
	coo2csr (A,A_mat);
	//sortCSR(A_mat);
	//printMatrix(A_mat);
	coo2csr (ilu,ilu_mat);
	//sortCSR(ilu_mat);
	freeCOO(A);
	freeCOO(ilu);
	printf ("Convert 2csr complete\n");
	//ilu0 and solve
	double *x = (double*)calloc(A_mat->n, sizeof(double));

	int key; //1 - block, 2 - usually
	int nb;

	if (device == 0)
	{
	    if (part > 1)
	    {
	        key = 1;
	        nb = part;
	    }
	    else 
		key = 2;
	}
	else if (part == 1)
	{
	    key = 3;
	}
	else 
	    {
			key = 4;
			nb = part;
	    }
	
	if ( key == 1 )
	{
		printf ("Usage block ilu0(%d)\n",nb);	
		/* Partition Matrix for ILU0 */
		double time_partition = omp_get_wtime();
		csr_t** bilu = partition(ilu_mat,nb,dimb);
		printf ("time partition %lf\n",omp_get_wtime()-time_partition);
		double time_bilu = omp_get_wtime();
		int err = bilu0(bilu,nb);
		printf ("time bilu0 %lf\n",omp_get_wtime()-time_bilu);
		csr_zero2one_index(A_mat);
		double time_solve = omp_get_wtime();
		err = solve_bicgstab_block(A_mat,bilu,nb, b, x);	
		printf ("Time Solve %lf\n",omp_get_wtime()-time_solve);
		for (int i = 0; i < nb; i++)
			freeCSR(bilu[i]);
	}
	else if ( key == 2 )
	{
		printf ("Usage ilu0\n");	
		double time_ilu = omp_get_wtime();
		int err = ilu0(ilu_mat);
		printf ("Time ILU0 %lf\n",omp_get_wtime()-time_ilu);
		csr_zero2one_index(A_mat);
		double time_solve = omp_get_wtime();
		err = solve_bicgstab(A_mat,ilu_mat, b, x);	
		printf ("Time Solve %lf\n",omp_get_wtime()-time_solve);
	}
	else if ( key == 3 )
	{
		printf ("Using block ilu0\n");
		init();
		int err = bicgstab_cusparse(A_mat,ilu_mat, b, x);
	}
	else if ( key == 4 )
	{
	    printf ("Using block ilu0(%d)\n",nb);	
	    /* Partition Matrix for ILU0 */
	    double time_partition = omp_get_wtime();
	    csr_t** bilu = partition(ilu_mat,nb,dimb);
	    csr_t** bA = partitionMatrix (A_mat,bilu,nb);
	    printf ("time partition %lf\n",omp_get_wtime()-time_partition);
	    //csr_zero2one_index(A_mat);
	    int err = bicgstab_fullblock_cusparse(bA, bilu, nb, b, x);
	    for (int i = 0; i < nb; i++)
	    {
			freeCSR(bilu[i]);
			freeCSR(bA[i]);
	    }
	}
	
	printVector(x,A_mat->n);
	
		
	freeCSR(A_mat);
	freeCSR(ilu_mat);
	free(A);
	free(ilu);
	free(A_mat);
	free(ilu_mat);
	free (b);
	free (x);
	return 0;
}