Exemplo n.º 1
0
// Main method
int main(int argc, char* argv[]) {
  int N;
  double elapsedTime;

  // checking parameters
  if (argc != 2 && argc != 4) {
    printf("Parameters: <N> [<fileA> <fileB>]\n");
    return 1;
  }
  N = atoi(argv[1]);
  initMatrixes(N);

  // reading files (optional)
  if(argc == 4){
    readMatrixFile(A,N,argv[2]);
    readMatrixFile(B,N,argv[3]);
  } else {
    // Otherwise, generate two random matrix.
    for (int i=0; i<N; i++) {
      for (int j=0; j<N; j++) {
	A[i][j] = rand() % 5;
	B[i][j] = rand() % 5;
      }
    }
  }

  // Do simple multiplication and time it.
  timerStart();
  simpleMM(N);
  printf("Simple MM took %ld ms\n", timerStop());

  // Do strassen multiplication and time it.
  timerStart();
  strassenMM(N);
  printf("Strassen MM took %ld ms\n", timerStop());

  if (compareMatrix(C, R, N) != 0) {
    if (N < 20) {
      printf("\n\n------- MATRIX C\n");
      printMatrix(C,N);
      printf("\n------- MATRIX R\n");
      printMatrix(R,N);
    }
    printf("Matrix C doesn't match Matrix R, if N < 20 they will be printed above.\n");
  }

  // stopping timer
  
  cleanup();
  return 0;
}
Exemplo n.º 2
0
int main(int argc, char *argv[], char *envp[])
{
	double 			*A, *B, *C;
	double 			elapsed = 0, totalElapsed = 0;
	double 			gflops = 0;
	int 			i, j, k;
	unsigned int	start = 1, end = 1, skip = 1, count = 1;
	unsigned int	size = 1, algorithm = 1, debug = 0;
	char 			c;

	struct timespec 	startTime, endTime;
	struct rusage 		ruStart, ruEnd;

	while((c = getopt(argc, argv, "c:s:e:j:a:d:")) != -1)
		switch(c) {
			case 's': 
				start = atoll(optarg);
				break;
			case 'e': 
				end = atoll(optarg);
				break;
			case 'j': 
				skip = atoll(optarg);
				break;
			case 'c':
				count = atoll(optarg);
				break;
			case 'a':
				algorithm = atoll(optarg);
				break;
			case 'd':
				debug = atoll(optarg);
				break;
			default:
				goto error0;
		}
	if(debug > 0) fprintf(stderr, "start = %d, end = %d, skip = %d, count = %d\n",
		start, end, skip, count);
	for(size=start; size<=end; size+=skip) {
		if(debug > 0) fprintf(stderr, "Starting size = %d\n", size);
		totalElapsed = 0;
		for(j=0; j<count; j++) {
			if(debug > 0) fprintf(stderr, "Computing matrix count = %d\n", j);
			A = calloc(size * size, sizeof(double));
			B = calloc(size * size, sizeof(double));
			C = calloc(size * size, sizeof(double));
			for(k=0; k<size * size; k++) {
				A[k] = drand48();
				B[k] = drand48();
			}
#ifdef HAVE_CLOCK_GETTIME
			clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &startTime);
#else
			getrusage(RUSAGE_SELF, &ruStart);
			startTime.tv_sec = ruStart.ru_utime.tv_sec + ruStart.ru_stime.tv_sec;
			startTime.tv_nsec = (ruStart.ru_utime.tv_usec + ruStart.ru_stime.tv_usec)*1000;
#endif
			switch(algorithm) {
				case 1:
					dMM(A, B, C, size, size, size);
					break;
				case 2:
					dMMT(A, B, C, size, size, size);
					break;
				case 3:
					dMMT2(A, B, C, size, size, size);
					break;
				case 4:
					dMMT2r(A, B, C, size, size, size);
					break;
				case 5:
					strassenMM(A, B, C, size, size, size);
					break;
				default:
					goto error1;
			}

#ifdef HAVE_CLOCK_GETTIME
			clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &endTime);
#else
			getrusage(RUSAGE_SELF, &ruEnd);
			endTime.tv_sec = ruEnd.ru_utime.tv_sec + ruEnd.ru_stime.tv_sec;
			endTime.tv_nsec = (ruEnd.ru_utime.tv_usec + ruEnd.ru_stime.tv_usec) * 1000;
#endif
			elapsed =  endTime.tv_sec;
			elapsed += (double)endTime.tv_nsec / 10e9;
			elapsed -= startTime.tv_sec;
			elapsed -= (double)startTime.tv_nsec / 10e9;
			totalElapsed += elapsed;
			if(debug > 0) {
				fprintf(stderr, "size = %d, alg = %d, start = %f, end = %f, elapsed = %f, Gops = %f\n",
					size, algorithm, 
					startTime.tv_sec + (double)(startTime.tv_nsec)/1e9,
					endTime.tv_sec + (double)(endTime.tv_nsec)/1e9,
					elapsed, ((double)2.0*size*size*size-size*size)/10e9
				);
			}
			free(A);
			free(B);
			free(C);		
		}
		gflops = ((((double)2.0*size*size*size-size*size))/10e9)/totalElapsed;
		printf("%u %u %f\n", size, count, gflops);
	}
	return(0);
error1:
	fprintf(stderr, "Unknown algorithm\n");
	exit(-1);
error0:
	fprintf(stderr, "Usage: %s -n N\n", argv[0]);
	fprintf(stderr, "N = matrix size\n");
	exit(-1);
}
Exemplo n.º 3
0
int main() {
    int row, col, i;
    time_t seed; // for random 
    srand((unsigned) time(&seed));

    m1 = (double **) malloc(sizeof (double *) * MAX);
    for (i = 0; i < MAX; i++)
        m1[i] = (double *) malloc(sizeof (double) * MAX);

    m2 = (double **) malloc(sizeof (double *) * MAX);
    for (i = 0; i < MAX; i++)
        m2[i] = (double *) malloc(sizeof (double) * MAX);

    for (row = 0; row < MAX; row++)
        for (col = 0; col < MAX; col++)
            m1[row][col] = rand() % LARGEST;
    for (row = 0; row < MAX; row++)
        for (col = 0; col < MAX; col++)
            m2[row][col] = rand() % LARGEST;

    int exponent = 1;
    while (pow(2, exponent) < MAX) // compare 2^n to see if it is < than input size, if it is then increment exponent.
        exponent++;

    int c;
    c = pow(2, exponent); //c = 2^n
    perfectMatrix = 2;

    if (c >= MAX) 
        perfectMatrix = c;

    pm1 = (double **) malloc(sizeof (double *) * perfectMatrix);
    for (i = 0; i < perfectMatrix; i++)
        pm1[i] = (double *) malloc(sizeof (double) * perfectMatrix);

    pm2 = (double **) malloc(sizeof (double *) * perfectMatrix);
    for (i = 0; i < perfectMatrix; i++)
        pm2[i] = (double *) malloc(sizeof (double) * perfectMatrix);

    pm3 = (double **) malloc(sizeof (double *) * perfectMatrix);
    for (i = 0; i < perfectMatrix; i++)
        pm3[i] = (double *) malloc(sizeof (double) * perfectMatrix);

    // all trailling zeros
    for (row = 0; row < perfectMatrix; row++) {
        for (col = 0; col < perfectMatrix; col++) {
            pm1[row][col] = 0;
            pm2[row][col] = 0;
            pm3[row][col] = 0;
        }
    }

    for (row = 0; row < MAX; row++) {
        for (col = 0; col < MAX; col++) {
            pm1[row][col] = m1[row][col];
            pm2[row][col] = m2[row][col];
        }
    }
  
    initMatrixes(perfectMatrix);
    printf("Done initiate\n");

    time_t start = time(NULL);

    strassenMM(perfectMatrix);

    //printf("Matrix Perfect Size = %d\n",perfectMatrix);
    printf("Calculation Time:%.2f\n", (double)(time(NULL) - start));    
    //print_perfect_matrices();
    return 0;
}