Parallel map

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Parallel map

Francesco Montanari-2

I'm trying to implement a simple parallel map.

(I'm aware about the MPI egg, it actually works fine for me, but I'd
like to have something more lightweight. I managed to write a
parallel map with the hardwood egg, but since it uses green threads it
is not convenient for my use cases. I had a look to past discussions
[1, 2] where POSIX threads were recommended, but I am not familiar
with them and I didn't manage to get the code snippets working.)


So, I tried to use openmp. I list a code example at the bottom of the
email. Compilation `csc -L -fopenmp test.scm` is ok and openmp seems
linked correctly, I can call functions defined in <omp.h>:

(omp_get_max_threads) ; 8

But _OPENMP is not enabled:

(is_omp_enabled) ; #f

The openmp pragma directives do not complain during compilation, but
only 1 thread is used in the parallel loop:

(import srfi-1)
(let* ((input (list->f64vector (iota 10)))
        (size (f64vector-length input))
        (output (make-f64vector size)))
   (squaremap size input output)
   output)                               ; prints nthreads: 1

Do CHICKEN green threads also limit the number of threads from code
called through the C FFI? If not, do you have suggestions about how to
proceed to enable openmp to use several threads?

(I checked that if I ran the C code directly with GCC then _OPENMP is
defined and several threads are used.)


(import (chicken foreign) srfi-4)

#include <omp.h>
#include <stdbool.h>

bool is_omp_enabled()
   bool omp = false;
#ifdef _OPENMP
   omp = true;
   return omp;

void squaremap(int n, double *a, double *b)
   int i;
#pragma omp parallel for
   for (i=0; i<n; i++)
       printf("nthreads: %d\n", omp_get_num_threads());
       b[i] = a[i]*a[i];

;; Is OpenMP enabled?
(define is_omp_enabled
   (foreign-lambda bool "is_omp_enabled"))

;; Maximum number of available threads.
(define omp_get_max_threads
   (foreign-lambda int "omp_get_max_threads"))

;; Compute squares of vector items..
(define squaremap
   (foreign-lambda void "squaremap" int f64vector f64vector))

Chicken-users mailing list
[hidden email]