Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <omp.h>
- #include <random>
- using namespace std;
- #pragma comment(linker, "/STACK:1024000000")
- int main()
- {
- /*int N = 100;
- int n = 1;
- double sum = 0.0;
- double start, end;
- start = omp_get_wtime();
- #pragma omp parallel for private(n) reduction(+:sum) num_threads(8) schedule(guided, 10000)
- for(n = 1; n <= N; n++)
- {
- sum+= pow(-1, n) / (pow((2 * n + 1), 3) - 1);
- }
- end = omp_get_wtime();
- cout << "Sum = " << sum << endl;
- cout << "Num Thread = " << omp_get_thread_num() << endl;
- cout << "Work took seconds " << end - start;*/
- srand(time(0));
- cout << "count repeat" << endl;
- int q;
- cin >> q;
- const int N = 5;
- int i, j;
- float A[N][N];
- float B[N][N];
- float C[N][N];
- float sum;
- for (i = 0; i < N; i++) {
- for (j = 0; j < N; j++) {
- A[i][j] = (floorf((rand() * 1.0 / RAND_MAX - 0.5) * 100)) / 100;
- B[i][j] = (floorf((rand() * 1.0 / RAND_MAX - 0.5) * 100)) / 100;
- C[i][j] = 0.0;
- }
- }
- double start, end;
- start = omp_get_wtime();
- for (int l = 0; l < q; l++)
- for (i = 0; i < N; i++) {
- for (j = 0; j < N; j++) {
- sum = 0;
- for (int k = 0; k < N; k++)
- sum += A[i][k] * B[k][j];
- C[i][j] = sum;
- }
- }
- end = omp_get_wtime();
- cout << "Matrix C:" << endl << C[0][0] << " " << C[N - 1][0] << endl << C[0][N - 1] << " " << C[N - 1][N - 1] << endl;
- cout << "Work took seconds non parallel " << end - start << endl;
- start = omp_get_wtime();
- for (int l = 0; l < q; l++)
- {
- #pragma omp parallel for private(i,j)
- for (i = 0; i < N; i++) {
- for (j = 0; j < N; j++) {
- sum = 0;
- for (int k = 0; k < N; k++)
- sum += A[i][k] * B[k][j];
- C[i][j] = sum;
- }
- }
- }
- end = omp_get_wtime();
- cout << "Matrix C:" << endl << C[0][0] << " " << C[N - 1][0] << endl << C[0][N - 1] << " " << C[N - 1][N - 1] << endl;
- cout << "Work took seconds parallel i " << end - start << endl;
- start = omp_get_wtime();
- for (int l = 0; l < q; l++)
- {
- for (i = 0; i < N; i++) {
- #pragma omp parallel for
- for (j = 0; j < N; j++) {
- sum = 0;
- for (int k = 0; k < N; k++)
- sum += A[i][k] * B[k][j];
- C[i][j] = sum;
- }
- }
- }
- end = omp_get_wtime();
- cout << "Matrix C:" << endl << C[0][0] << " " << C[N - 1][0] << endl << C[0][N - 1] << " " << C[N - 1][N - 1] << endl;
- cout << "Work took seconds parallel j " << end - start << endl;
- start = omp_get_wtime();
- for (int l = 0; l < q; l++)
- {
- for (i = 0; i < N; i++) {
- for (j = 0; j < N; j++) {
- sum = 0;
- #pragma omp parallel for reduction(+:sum)
- for (int k = 0; k < N; k++)
- sum += A[i][k] * B[k][j];
- C[i][j] = sum;
- }
- }
- }
- end = omp_get_wtime();
- cout << "Matrix C:" << endl << C[0][0] << " " << C[N - 1][0] << endl << C[0][N - 1] << " " << C[N - 1][N - 1] << endl;
- cout << "Work took seconds parallel k " << end - start << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement