chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Parallel map


From: Francesco Montanari
Subject: [Chicken-users] Parallel map
Date: Sun, 31 Mar 2019 23:52:11 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1

Hi,

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.)

[1] https://lists.nongnu.org/archive/html/chicken-users/2010-12/msg00213.html [2] https://lists.nongnu.org/archive/html/chicken-users/2016-12/msg00017.html

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.)

Best,
Francesco



(import (chicken foreign) srfi-4)

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

bool is_omp_enabled()
{
  bool omp = false;
#ifdef _OPENMP
  omp = true;
#endif
  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))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]