[clean-list] Calling C with side effects
Matthew Bromberg
mattcbro at earthlink.net
Fri May 12 20:42:48 MEST 2006
I am testing the FFI and am attempting to call C with some side affects
using the technique shown in one of the htoclean examples. I am running
windows XP 64, but all the
code is 32 bit of course.
Unfortunately I can not get the side effect to manifest, even though (I
think)
the C function gets called.
Here is the clean code:
module matrix
import StdEnv
import StdList
import StdArray
rmac :: !Real !{#Real} !{#Real} -> Int;
rmac a0 a1 a2 = code {
ccall rmac "RAA:I"
}
x :: {#Real}
x = {0.5, -0.5, 1.0, -1.0}
// unboxed array IO, uniqueness
y :: *{#Real}
y = {1.0, 2.0, 3.0, 4.0}
nlen :: *Int
nlen = 0
Start :: (*{#Real}, Int)
Start
#! nlen = rmac 2.0 x y
= (y, nlen)
This code outputs
({1,2,3,4},4)
Thus y has not changed and the side effect I was looking for in rmac did
not occur even though nlen is
correctly altered!. Yet it seems a very similar technique is applied in
the example_string1 example in htoclean.
Here is the C code for rmac
#include "acml.h"
#include "Clean.h"
#include "cmatrix.h"
#include "stdio.h"
/* real multiply and accumulate using a stride of 1
* y = y + alpha * x
* the array length is returned */
int rmac(double alpha, CleanRealArray x, CleanRealArray y )
{
int nlen ;
int k ;
nlen = CleanRealArraySize(y);
for (k = 0 ; k < nlen ; k++) {
y[k] = y[k] + alpha * x[k] ;
// printf("y[%d]: %g\n", k, y[k]) ;
}
// daxpy(nlen, alpha, x, 1, y, 1) ;
return(nlen) ;
}
By the way, I compile this to a .dll, create a symbol file and then link
the dll in. If I attempt to link in routines from other dlls I always
get into trouble, either it can't find them or the link creates an
unusable binary. ie if I uncomment the printf statement, the linker can
not find printf even though the system libraries should all be on the
path; or if I try to link in daxpy from the acml BLAS library I get a
binary that fails to launch. The linker seems to have some issues with
dynamic link libraries and unfortunately it doesn't support static
libraries. I assume I'm just doing something wrong, since obviously the
gui libraries are hooking into a bunch of windows .dlls.
More information about the clean-list
mailing list