2010/4/15 "Miguel A. Vázquez" <mavazq...@cttc.es>:
> Hello all,
>
> I am trying to implement some code in OpenMP under Octave, but I have
> some problems.
>
> #include <omp.h>
> #include <octave/oct.h>
>
>     DEFUN_DLD (pmean, args, , "Calculates then mean in parallel")
>     {
>       int nargin = args.length ();
>
>       Matrix m (1,1);
>
>
>       int tid,nthreads;
>       omp_set_num_threads(2);
>       #pragma omp parallel private( nthreads, tid)
>       tid = omp_get_thread_num();
>       nthreads = omp_get_num_threads();
>       octave_stdout << "Thread "<< tid << " of " << nthreads << "
> threads \n.";
>       if (nargin != 1)
>         print_usage ();
>       else
>         {
>           NDArray A = args(0).array_value ();
>
>
>
>        #pragma omp parallel for reduction(+:m)
>        for (octave_idx_type i = 0; i < A.nelem (); i++){
>                       m = m + A.elem (i)/A.nelem ();
>        }
>
>                 return octave_value (m);
>
>         }
>
>     }
>
> This code is supposed to calculate a mean of an Octave matrix in
> parallel: some iterations of the for will be done by different threads.
>
> I set the number of threads to 2 (omp_set_num_threads(2)). I compiled
> and linked it nicely but, unfortunatelly, when I executed it:
>
> octave:1> pmean([10 0 5])
> Thread 0 of 1 threads
> .ans =  5
>
> One thread is executed instead. I don't really know how to solve this
> problem, even don't know where I have to look for. Does anyone of you
> have treated this problem before?
>
> Thank you in advance,
>
> Miguel
>

I think you lack some basic OpenMP knowledge. In C++, omp parallel
applies only to the next statement or block, so the way you've written
it it doesn't make much sense. Use omp critical for I/O to make it
atomic.
You didn't initialize m; besides, you must use a primitive type for
reduction clause. Also be aware that each DEFUN must return a value or
throw an exception, just like every C++ function.

This version works for me:

DEFUN_DLD (pmean, args, , "Calculates then mean in parallel")
{
  int nargin = args.length ();
  octave_value retval;

  omp_set_num_threads(2);

#pragma omp parallel
    {
      int tid,nthreads;
      tid = omp_get_thread_num();
      nthreads = omp_get_num_threads();
      // WARNING: I/O not thread safe
#pragma omp critical
      octave_stdout << "Thread "<< tid << " of " << nthreads << " threads. \n";
    }

  if (nargin != 1)
    print_usage ();
  else
    {
      const NDArray A = args(0).array_value ();

      double m = 0;
#pragma omp parallel for reduction(+:m)
      for (octave_idx_type i = 0; i < A.nelem (); i++)
        m += A.elem (i) / A.nelem ();

      retval = m;
    }

  return retval;
}

hth

-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to