Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <omp.h>
- #include <sys/time.h>
- #define N 3200
- void multiplyMatrices(int first[][N], int second[][N], int result[][N])
- {
- // Multiplying first and second matrices and storing it in result
- int i,j,k;
- #pragma omp parallel for
- for (i = 0; i < N; ++i) {
- for (j = 0; j < N; ++j) {
- for (k = 0; k < N; ++k) {
- result[i][j] += first[i][k] * second[k][j];
- }
- }
- }
- }
- void fill_matrices(int first[][N], int second[][N], int result[][N])
- {
- srand(time(NULL)); // randomize seed
- for (int i = 0; i < N; i++){
- for (int j = 0; j < N; j++){
- first[i][j] = rand() % 10;
- second[i][j] = rand() % 10;
- result[i][j] = 0;
- }
- }
- }
- void print(int first[][N], int second[][N], int result[][N])
- {
- srand48(time(NULL)); // randomize seed
- printf("First:\n");
- for (int i = 0; i < N; i++){
- printf("[ ");
- for (int j = 0; j < N; j++){
- printf("%d ", first[i][j]);
- }
- printf("]\n");
- }
- printf("\nSecond:\n");
- for (int i = 0; i < N; i++){
- printf("[ ");
- for (int j = 0; j < N; j++){
- printf("%d ", second[i][j]);
- }
- printf("]\n");
- }
- printf("\nResult:\n");
- for (int i = 0; i < N; i++){
- printf("[ ");
- for (int j = 0; j < N; j++){
- printf("%d ", result[i][j]);
- }
- printf("]\n");
- }
- }
- int main()
- {
- double start1, start2, stop1, stop2, execution_time1, execution_time2;
- omp_set_num_threads(omp_get_num_procs());
- int first[N][N], second[N][N], result[N][N];
- start1 = omp_get_wtime();
- fill_matrices(first, second, result);
- stop1 = omp_get_wtime();
- start2 = omp_get_wtime();
- multiplyMatrices(first, second, result);
- stop2 = omp_get_wtime();
- execution_time1 = stop2 - start1;
- execution_time2 = stop2 - start2;
- //print(first, second, result);
- printf("Total execution Time in seconds: %.10lf\n", execution_time1 );
- printf("MM execution Time in seconds: %.10lf\n", execution_time2 );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement