A
A
adam_carraway2018-10-16 21:05:39
C++ / C#
adam_carraway, 2018-10-16 21:05:39

How to parallelize given openmp code?

#include<iostream>
#include <sys/time.h>
#include <cmath>
#include "omp.h"

using namespace std;

int const N = 4;
int X[N]; 
long count1;


bool Check(int K, int M) 
{
  int i = 0;
  while ((i< K) && (M != X[i]) && (abs(K - i) != abs(M - X[i]))) i++;
  return (i == K);
}

void MainProc(int k)
{
int m;
    for (m=0; m<N; m++)

      if (Check(k, m))
      {
        
        X[k] = m;

        if (k == (N - 1)) 
        {
          count1++; 
        }
        MainProc(k + 1); 
  }
}


int main() 
{
  double iv;
  struct timeval st, et;
  gettimeofday(&st, NULL); 
  MainProc(0);
  gettimeofday(&et, NULL);
    iv = (et.tv_sec * 1.0e6 + et.tv_usec) - (st.tv_sec * 1.0e6 + st.tv_usec);
  setlocale(LC_ALL, "Russian");
  cout << "При N=" << N << " количество расстановок: " << count1 << endl<<endl;
  cout<<"Time: "<< iv / 1e6 <<"sec." << endl;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question