Logo

dev-resources.site

for different kinds of informations.

How to use OpenMP from Perl with SPVM.

Published at
3/28/2024
Categories
perl
openmp
spvm
Author
yukikimoto
Categories
3 categories in total
perl
open
openmp
open
spvm
open
Author
10 person written this
yukikimoto
open
How to use OpenMP from Perl with SPVM.

How to use OpenMP from Perl with SPVM.

See an example to bind OpenMP to SPVM

SPVM provides a way to bind C language libraries and call them from Perl.

When binding C libraries to Perl, you typically write XS, but SPVM offers an alternative approach.

MyOpenMP.spvm

class MyOpenMP {

  native static method sum_vec_int : int[] ($nums1 : int[], $nums2 : int[]);

  static method test : void () {
    my $nums1 = [1, 2, 3];
    my $nums2 = [5, 6, 7];

    my $nums3 = &sum_vec_int($nums1, $nums2);

    for (my $i = 0; $i < @$nums3; $i++) {
      say $nums3->[$i];
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

MyOpenMP.c

#include "spvm_native.h"

#include <string.h>

static const char* FILE_NAME = "SPVM/MyOpenMP.c";

int32_t SPVM__MyOpenMP__sum_vec_int(SPVM_ENV* env, SPVM_VALUE* stack) {

  void* obj_nums1 = stack[0].oval;
  if (obj_nums1 == NULL) {
    return env->die(env, stack, "$nums1 must be defined.", __func__, FILE_NAME, __LINE__);
  }
  int32_t* nums1 = env->get_elems_int(env, stack, obj_nums1);

  void* obj_nums2 = stack[1].oval;
  if(obj_nums2 == NULL) {
    return env->die(env, stack, "$nums2 must be defined.", __func__, FILE_NAME, __LINE__);
  }
  int32_t* nums2 = env->get_elems_int(env, stack, obj_nums2);

  int32_t length = env->length(env, stack, obj_nums1);

  void* obj_nums3 = env->new_int_array(env, stack, length);
  int32_t* nums3 = env->get_elems_int(env, stack, obj_nums3);

  int32_t i;
#pragma omp parallel for
  for (i = 0; i < length; i++) {
    nums3[i] = nums1[i] + nums2[i];
  }

  stack[0].oval = obj_nums3;

  return 0;
}
Enter fullscreen mode Exit fullscreen mode

MyOpenMP.config

use strict;
use warnings;

use SPVM::Builder::Config;

my $config = SPVM::Builder::Config->new_gnu99(file => __FILE__);

# Compiler options
$config->add_ccflag('-fopenmp');

# Linker option
$config->add_ldflag('-fopenmp');
$config->add_lib('gomp');

$config;
Enter fullscreen mode Exit fullscreen mode

openmp.pl

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/lib";

use SPVM 'MyOpenMP';

SPVM::MyOpenMP->test;
Enter fullscreen mode Exit fullscreen mode

This program outputs values calculated by OpenMP.

6
8
10
Enter fullscreen mode Exit fullscreen mode
spvm Article's
30 articles in total
Favicon
How to create Mac GUI applications in SPVM?
Favicon
How to create a parallel echo server using goroutines in SPVM?
Favicon
How to generate an executable file for an AI program using SPVM ?
Favicon
SPVM::R - A Port of The R Language Features.
Favicon
SPVM::Resource::Eigen released
Favicon
SPVM Documentation
Favicon
How to use OpenMP from Perl with SPVM.
Favicon
How to use zlib (a C library) from Perl with SPVM
Favicon
SPVM::Digest::MD5 - MD5
Favicon
SPVM::MIME::Base64 - Base64 Encoding/Decoding
Favicon
SPVM::MIME::QuotedPrint - Quoted-Printable encoding/decoding
Favicon
SPVM::Math - Mathematical Functions
Favicon
SPVM
Favicon
SPVM::Sys now supports symbolic links on Windows, adds Perl-compatible API.
Favicon
SPVM now supports object-oriented programming in Perl
Favicon
First release of SPVM::Resource::RE2 Resourcing the regular expression library Google RE2
Favicon
First release of SPVM::File::Temp and SPVM::File::Find
Favicon
SPVM::File::Path and SPVM::File::Glob are newly released.
Favicon
First release of SPVM::File::Copy and SPVM::FindBin
Favicon
First release of SPVM::File::Spec - complex regular expressions, file tests, SPVM::Cwd, inheritance
Favicon
SPVM::File::Basename is released. This is the first module of SPVM using regular expressions.
Favicon
SPVM improved Exchange API at v0.9684. Welcome to this easy world of type conversion!
Favicon
SPVM supported self Compiler in 0.9683.
Favicon
SPVM 0.9680 is released
Favicon
SPVM 0.9677 is released
Favicon
SPVM 0.9676 is released
Favicon
SPVM 0.9672 is released
Favicon
SPVM 0.9670 is released
Favicon
SPVM 0.9669 is released
Favicon
SPVM 0.9668 is released

Featured ones: