1

I'm trying to perform matrix multiplication using openMP as follows and I compile it using GCC : g++ -std=gnu++11 -g -Wall -fopenmp -o parallel_not_opt parallel_not_opt.cpp

But when I try to run it by using parallel_not_opt.exe, it aborts giving the typical Windows error parallel_not_opt.exe has stopped working...

Am I missing something?

#include "includes/stdafx.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <vector> # include <omp.h> #include <chrono> #include <fstream> #include <algorithm> #include <immintrin.h> #include <cfloat> #include <limits> #include <math.h> using namespace std::chrono; using namespace std; //populate matrix with random values. double** generateMatrix(int n){ double max = DBL_MAX; double min = DBL_MIN; double** matA = new double*[n]; for (int i = 0; i < n; i++) { matA[i] = new double[n]; for (int j = 0; j < n; j++) { double randVal = (double)rand() / RAND_MAX; matA[i][j] = min + randVal * (max - min); } } return matA; } //generate matrix for final result. double** generateMatrixFinal(int n){ double** matA = new double*[n]; for (int i = 0; i < n; i++) { matA[i] = new double[n]; for (int j = 0; j < n; j++) { matA[i][j] = 0; } } return matA; } //matrix multiplication - parallel double matrixMultiplicationParallel(double** A, double** B, double** C, int n){ int i, j, k; clock_t begin_time = clock(); # pragma omp parallel shared ( A,B,C,n ) // private ( i, j, k ) { # pragma omp for for (i = 0; i < n; i++) { // cout<< i << ", " ; for (j = 0; j < n; j++) { for (k = 0; k < n; k++) { C[i][j] += A[i][k] * B[k][j]; } } } } double t = float(clock() - begin_time); return t; } int _tmain(int argc, _TCHAR* argv[]) { ofstream out("output.txt", ios::out | ios::app); out << "--------------STARTED--------------" << "\n"; int start = 200, stop = 2000, step = 200; for (int n = start; n <= stop; n += step) { srand(time(NULL)); cout << "\nn: " << n << "\n"; double t1 = 0; int my_size = n; double **A = generateMatrix(my_size); double **B = generateMatrix(my_size); double **C = generateMatrixFinal(my_size); double single_sample_time = matrixMultiplicationParallel(A, B, C, n); t1 += single_sample_time; for (int i = 0; i < n; i++) { delete[] A[i]; delete[] B[i]; delete[] C[i]; } delete[] A; delete[] B; delete[] C; } out << "-----------FINISHED-----------------" << "\n"; out.close(); return 0; } 
7
  • 2
    Use a debugger and pinpoint the error. It is hard to find the error without debugging it. And one more thing, check whether your compiler supports openMP. Not all compilers implement openMP. Commented Jul 10, 2017 at 14:37
  • @ShanilFernando But it runs correctly in Visual Studio, I'm trying to run it using CMD and get this error. Commented Jul 10, 2017 at 14:45
  • Did compile it to release? Visual studio compiler is not gcc by default Commented Jul 10, 2017 at 14:58
  • 1
    Are you familiar with gdb? Use it and debug or wait for few hours until i go back home and help you to debug it. Commented Jul 10, 2017 at 15:07
  • 1
    I saw his answer. I'm glad that you found the bug, Anyway I really admire someone from my country using openmp. I have a one recommendation for you, try to implement matrix calculations with CUDA (GPU accelerated). you will be amazed how much performance gain you can achieve. Specially for real time applications with more than 5,000,000 matrix calculations per second is involved. Commented Jul 10, 2017 at 17:03

1 Answer 1

2

The private ( i, j, k ) declaration is not optional. Add it back, otherwise the inner loop variables j and k are shared, which completely messes up the inner loops.

It is better to declare variables as locally as possible. That makes reasoning about OpenMP code much easier:

clock_t begin_time = clock(); # pragma omp parallel { # pragma omp for for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { C[i][j] += A[i][k] * B[k][j]; } } } } return float(clock() - begin_time); 

In that case, A,B,C will be shared by default - coming from the outside, and j,k are private because they are declared within the parallel scope. The loop variable of a parallel for is always implicitly private.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.