[clean-list] Sub-arrays How do I manipulate an array pointer?

Matthew Bromberg mattcbro at earthlink.net
Fri Jun 2 22:54:30 MEST 2006


Some of these interface issues are making me think I should push more of 
the matrix functionality into C.
In fact, my current thinking is to implement a kind of stacked based 
matrix virtual machine.
I'll call it the abcd  machine for "Already Bitten and Chewed Data".  
(I'm kidding about the name).

 I can use a functional language to drive the
matrix computational engine, but minimize somewhat the passing of data 
between C and clean.  This reduces
the exposure of Clean to imperative programming.
The C code can read data in from disk or matlab files etc., scripted by 
a higher order language.

It would be nice though to use Clean's array comprehensions to 
initialize or alter values held in a matrix.
Are there any nice hacks for processing array data held in C but not 
managed by Clean? Can the
garbage collector be told to leave an array alone?  I think it's 
possible already to return data from a
C array, but it's copied as soon as it gets into Clean I believe. (At 
least it works that way for strings I'm told.)

Equivalently if I could have a C enabled function pointer from Clean 
that could be used to process raw data,
that would be pretty useful.  In fact even if I could get very simple 
functions  like

Int Int -> Int or
Double -> Double

from Clean into C it could be quite useful.  These functions would 
probably be curried with lots of configuration data.

Regards





John van Groningen wrote:
> Matthew Bromberg wrote:
>   
>> I am not sure how to proceed concerning manipulating pointers to arrays in
>> clean.
>> In particular I need to pass to the C code, not the start of an unboxed
>> array, but rather some
>> offset from the beginning,
>>     
>
> That is not possible with the current implementation of ccall. However
> it is possible to add the offset using abc instructions just before
> the ccall instruction. For example:
>
> daxpy_oy :: !Int !Real !{#Real} !Int !{#Real} !Int !Int !*Real -> *Real;
> daxpy_oy nlen alpha x x_d y y_o y_d s = code {
> 	| y   at offset 1 on the A stack
> 	| y_o at offset 4 on the B stack
> 	push_b 4		| y_o
> 	push_a_b 1		| y
>
> 	pushI 3
> 	push_b 2
> 	shiftl%
> 	addI
> 	push_b_a 0
> 	pop_b 2
>
> 	update_a 0 2	| y+y_o<<3
> 	pop_a 1
>
> 	| remove y_0
> 	update_b 3 4
> 	update_b 2 3
> 	update_b 1 2
> 	update_b 0 1
> 	pop_b 1
>
> 	ccall daxpy "IRAIAI:V:R"
> }
>
> daxpy_oxy :: !Int !Real !{#Real} !Int !Int !{#Real} !Int !Int !*Real -> *Real;
> daxpy_oxy nlen alpha x x_o x_d y y_o y_d s = code {
> 	| x   at offset 0 on the A stack
> 	| y   at offset 1 on the A stack
> 	| x_o at offset 3 on the B stack
> 	| y_o at offset 5 on the B stack
>
> 	push_b 3		| x_o
> 	push_a_b 0		| x
>
> 	pushI 3
> 	push_b 2
> 	shiftl%
> 	addI
> 	push_b_a 0
> 	pop_b 2
>
> 	update_a 0 1	| x+x_o<<3
> 	pop_a 1
>
> 	push_b 6		| y_o
> 	push_a_b 1		| y
>
> 	pushI 3
> 	push_b 2
> 	shiftl%
> 	addI
> 	push_b_a 0
> 	pop_b 2
>
> 	update_a 0 2	| y+y_o<<3
> 	pop_a 1
>
> 	| remove x_o and y_o
> 	update_b 4 5
> 	update_b 2 4
> 	update_b 1 3
> 	update_b 0 2
> 	pop_b 2
>
> 	ccall daxpy "IRAIAI:V:R"
> }
>
> Start :: (!{#Real},!{#Real},!Real);
> Start
> 	# st = 0.0;
> 	# r1 = { 1.0,  2.0,  3.0 , 4.0};
> //	# st = daxpy   4 2.0 {0.5, -0.5, 1.0, -1.0} 1 r1 1 st;
> 	# st = daxpy_oy 2 2.0 {0.5, -0.5, 1.0, -1.0} 1 r1 1 1 st;
> //	# st = daxpy_oxy 2 2.0 {0.5, -0.5, 1.0, -1.0} 1 1 r1 1 1 st;
> 	# r2 = {11.0, 12.0, 13.0, 14.0};
> //	#! st = daxpy 4 3.0 {0.5, -0.5, 1.0, -1.0} 1 r2 1 st;
> 	#! st = daxpy_oxy 2 3.0 {0.5, -0.5, 1.0, -1.0} 1 1 r2 1 1 st;
> 	= (r1,r2,st);
>
> The ccall could be extended to make this possible without all the abc
> instructions.
>
>   
>> or even better to just store the subarray, as an
>> array pointing
>> to the middle of another array,  if that were possible.
>>     
>
> This is not possible for arrays that are allocated in the heap of Clean.
>
>   
>> I notice in the htoclean documentation that there is a mention of treating C
>> pointers as integers, but I have no idea how to coerce an array of unboxed
>> doubles to an integer representing a pointer.  Is there a simple way to do
>> this?  In Haskell it is possible to add offsets to foreign pointers for
>> example.
>>     
>
> Pointers that point to memory allocated outside the Clean heap (for example
> by malloc in C) can be stored as an Int in Clean. Offsets can be added to
> those integers.
>
> Kind regards,
>
> John van Groningen
>
>   


More information about the clean-list mailing list