コード例 #1
0
//#define Debug
int main(int argc, char **argv)
{
	if(argc != 3)
	{
		printf("Usage: test {N}\n");
		exit(-1);
	}
	size_t N = atoi(argv[1]);
	size_t npro = atoi(argv[2]);
	omp_set_num_threads(npro);
	 int iam = 0, np = 1;
	 #pragma omp parallel private(iam, np)
          {
                  np = omp_get_num_threads();
                  iam = omp_get_thread_num();
#ifdef Debug
                  printf("Hello from thread %d out of %d\n", iam, np);
#endif
          }
	struct timeval tv1,tv2;
	// generate a random matrix.
	printf("Size is %d,numP is %d\n",N,npro);
	int *mat = (int*)malloc(sizeof(int)*N*N);
	GenMatrix(mat, N);

	// compute the reference result.
	int *ref = (int*)malloc(sizeof(int)*N*N);
	memcpy(ref, mat, sizeof(int)*N*N);
	gettimeofday(&tv1,NULL);
	ST_APSP(ref, N);
	gettimeofday(&tv2,NULL);
	printf("Sequential time = %ld usecs\n",
                          (tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec);


	// compute your results
	int *result = (int*)malloc(sizeof(int)*N*N);
	memcpy(result, mat, sizeof(int)*N*N);
//	ST_APSP(result, N);

	gettimeofday(&tv1,NULL);
        OMP_APSP(result,N);
        gettimeofday(&tv2,NULL);
        printf("OpenMp time = %ld usecs\n",
                          (tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec);

	// compare your result with reference result
	if(CmpArray(result, ref, N*N))
		printf("Your result is correct.\n");
	else
		printf("Your result is wrong.\n");
}
コード例 #2
0
void prepare_data(int** mat, int** ref, int** result, int mat_size) {
	//generate a random matrix.
	*mat = (int*)malloc(sizeof(int) * mat_size * mat_size);
	GenMatrix(*mat, mat_size);

	//compute the reference result.
	*ref = (int*)malloc(sizeof(int) * mat_size * mat_size);
	memcpy(*ref, *mat, sizeof(int) * mat_size * mat_size);
	// start sequential timer
	gettimeofday(&timer_sequential, NULL);
	ST_APSP(*ref, mat_size);
	// stop sequential timer
	time_used_sequential = get_time_and_replace(&timer_sequential);
	printf("Time used (sequential): %8ld usecs\n", time_used_sequential);

	// compute your result
	*result = (int*)malloc(sizeof(int) * mat_size * mat_size);
	memcpy(*result, *mat, sizeof(int) * mat_size * mat_size);
}
コード例 #3
0
int main(int argc, char **argv)
{
	if(argc != 2)
	{
		printf("Usage: test {N}\n");
		exit(-1);
	}

	size_t N = atoi(argv[1]);
	// matrix related variables
	int *mat, *ref, *result, *part, *k_row;
	int rows, k, root;
	int i, j, vij, vik, vkj;
	int npes, rank;
	struct timeval tv1, tv2;
	MPI_Init(&argc, &argv);
	MPI_Comm_size(MPI_COMM_WORLD, &npes);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	if(rank == 0){
		// generate matrix and compute sequential
		mat = (int*)malloc(sizeof(int)*N*N);
		ref = (int*)malloc(sizeof(int)*N*N);
		result = (int*)malloc(sizeof(int)*N*N);
		GenMatrix(mat, N);
		memcpy(ref, mat, sizeof(int)*N*N);
		gettimeofday(&tv1, NULL);
		ST_APSP(ref, N);
		gettimeofday(&tv2, NULL);
		printf("Sequential: %ld usecs\n",(tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec);
		gettimeofday(&tv1, NULL);
	}

	// scatter the matrix
	rows = N/npes;
	part = (int*)malloc(sizeof(int)*N*rows);
	MPI_Scatter(mat, N*rows, MPI_INT, part, N*rows, MPI_INT, 0, MPI_COMM_WORLD);

	// parallel computing
	k_row = (int*)malloc(sizeof(int)*N);
	for(k = 0; k < N; k++){
		root = k/rows;
		if(rank == root){
			for(i = 0; i < N; i ++){
				*(k_row+i) = *(part + N*(k-rows*root) + i);
			}
		}
		MPI_Bcast(k_row, N, MPI_INT, root, MPI_COMM_WORLD);
		for(i = 0; i < rows; i++){
			for(j = 0; j < N; j++){
				vij = *(part + N*i + j);
				vik = *(part + N*i + k);
				vkj = *(k_row + j);
				if(vik != -1 && vkj != -1){
					if(vij == -1 || vij > vik+vkj)
					*(part + N*i + j) = vik + vkj;	
				}
			}
		}
	}
	
	// gather the matrix
	MPI_Gather(part, N*rows, MPI_INT, result, N*rows, MPI_INT, 0, MPI_COMM_WORLD);

	//compare your result with reference result
	if(rank == 0){
		gettimeofday(&tv2, NULL);
		printf("Parallel: %ld usecs\n",(tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec);
//		printmat(mat, N);
//		printmat(ref, N);
//		printmat(result, N);
		if(CmpArray(result, ref, N*N))
			printf("Your result is correct.\n");
		else
			printf("Your result is wrong.\n");
	}

	// free memory
	if(rank == 0){
		free(mat);
		free(ref);
		free(result);
	}
	free(part);
	free(k_row);
	MPI_Finalize();
}
コード例 #4
0
int main(int argc, char *argv[]) {
	if(argc != 2)
	{
		printf("Missing Argument\n");
		exit(-1);
	}
	
	int i, j;
	int *mat, *ref;
	int P, myrank;
	size_t N = atoi(argv[1]); //matrix size
	struct timeval tv1,tv2;
	MPI_Init(&argc, &argv);
	MPI_Comm_size(MPI_COMM_WORLD, &P);
	MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
	
	
	if(myrank == 0) {
		// generate a random matrix.
		printf("input Size is %d; number of process is %d \n",N,P);
		mat = (int*)malloc(sizeof(int)*N*N);
		GenMatrix(mat, N);

		// compute the reference result.
		ref = (int*)malloc(sizeof(int)*N*N);
		memcpy(ref, mat, sizeof(int)*N*N);
		
		gettimeofday(&tv1, NULL);
		ST_APSP(ref, N);
		gettimeofday(&tv2, NULL);
		printf("Sequential time = %ld usecs\n", 
				(tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec);  
		
		// compute your results
		
	}
		int strip = N/P;
		int *part = (int*)malloc(sizeof(int)*N*strip); /*array holding the part for this processor*/
	// Scatter data to all processors
	MPI_Barrier(MPI_COMM_WORLD);
	    if (myrank == 0) {
                gettimeofday(&tv1,NULL);
        }
	MPI_Scatter(mat, N*strip, MPI_INT, part, N*strip, MPI_INT, 0, MPI_COMM_WORLD);

	// Compute matrix in parallel
	MT_APSP (part, MPI_COMM_WORLD, myrank, N, P);
	
	//Gather the results
	MPI_Gather(part, N*strip, MPI_INT, mat, N*strip, MPI_INT, 0, MPI_COMM_WORLD);
	MPI_Barrier(MPI_COMM_WORLD);
	if(myrank == 0) {
		gettimeofday(&tv2, NULL);
		printf("Parallel time = %ld usecs\n\n",
				(tv2.tv_sec-tv1.tv_sec)*1000000+tv2.tv_usec-tv1.tv_usec);
#ifdef test	
		// compare your result with reference result
		if(CmpArray(mat, ref, N*N))
			printf("Your result is correct.\n");
		else
			printf("Your result is wrong.\n");
#endif			
		free(mat);
		free(ref);
		
	}
	free(part);
	MPI_Finalize();
	return 0;
}