Calling C for vector operations

How would I interface to the following C routines

extern float x32(const float s, const long n, const float *x, const float *y);
extern double x64(const double s, const long n, const double *x, const double *y);

extern void y32(float *t, const long n, const float *x, const float *y);
extern void y64(double *t, const long n, const double *x, const double *y);

assuming I have

var x : [1..n] real(64), y[1.,.n] real(64);
var u : [20..40] real(32), v[20..40] real(32);

Thanks

Hi @damianmoz -

Here is an example you can play with:

// da.h
static float sumAcross32(const float s, const long n, const float *x) {
  float ret = s;
  for (long i = 0; i < n; i++) {
    ret += x[i];
  }
  return ret;
}

static double sumAcross64(const double s, const long n, const double *x) {
  double ret = s;
  for (long i = 0; i < n; i++) {
    ret += x[i];
  }
  return ret;
}
require "da.h"; // or name it on command line
use CPtr;
use SysCTypes;

extern proc sumAcross32(s: real(32), n: c_long, x: c_ptr(real(32))):real(32);
extern proc sumAcross64(s: real(64), n: c_long, x: c_ptr(real(64))):real(64);

proc sumAcross(s: ?t, arr: [] t) {
  // Note, this will not work for non-contiguous array storage:
  //  * multidimensional arrays might or might not work the way you want
  //  * arrays with strided domains probably don't do what you expect
  if t == real(32) {
    return sumAcross32(s:real(32), arr.size, c_ptrTo(arr));
  } else if t == real(64) {
    return sumAcross64(s:real(64), arr.size, c_ptrTo(arr));
  } else {
    compilerError("sumAcross not implemented yet for this type");
  }
}

var x : [1..10] real(64);
var sumX = sumAcross(1.0, x);
writeln(sumX);

var u : [20..40] real(32);
var sumU = sumAcross(1.0:real(32), u);
writeln(sumU);
chpl da.chpl
./da

See also C Interoperability — Chapel Documentation 1.25

Hope that helps,

-michael

Its exactly what needed. Thanks heaps.

I went looking for something like that in the documentation (or examples) but found nothing. Was that just poor searching on my part?

Thanks again.