Calling C from J 
Author Message
 Calling C from J

I am having trouble using the LinkJ interface on an SGI unix system.
I decided to start by trying the example in main.c as discussed in Appendix C
of Roger Hui's "An Implementation of J".

I modified js.h as follows:
#define LINKJ 1

The program compiled & linked OK. Then I tried the following test:

   jc=. 10!:0
   5 jc i. 7
0 1 2 3 0
   jc i. 7
7
   3 jc i. 3 4
1 2 _1

Thus the monadic jc seems OK, but the diadic jc fails. ("x jc y" should give
the first x elements of y.)

I tried debugging with dbx. The new array was generated correctly, but
something went wrong in the printing process. I had trouble following
what was happening.

I tried modifying the code to retain the normal main but still define
10!:0 (essentially as Emmett McLean did in J-lapack interface). I got the
same problem again.

Has anyone else had similar problems?  Any help would be much appreciated.

Should I report the problem to Iverson Software Inc.? If so, can they be
contacted by email?

There are two distinct parts to LinkJ, one for calling C from J & the other
for calling J from C. There should be two preprocessor flags to control
these separately, rather than just LINKJ which enables/disable both
together.
--
Harvey Davies,
CSIRO Division of Atmospheric Research,    Voice: +61 3 586 7574
Private Bag No. 1, Mordialloc,               Fax: +61 3 586 7600



Mon, 29 Jan 1996 16:16:21 GMT  
 Calling C from J
In article (DAVIES Harvey) writes:
Quote:
>I am having trouble using the LinkJ interface on an SGI unix system.
>I decided to start by trying the example in main.c as discussed in Appendix C
>of Roger Hui's "An Implementation of J".

>I modified js.h as follows:
>#define LINKJ 1

>The program compiled & linked OK. Then I tried the following test:

>   jc=. 10!:0
>   5 jc i. 7
>0 1 2 3 0
>   jc i. 7
>7
>   3 jc i. 3 4
>1 2 _1

>Thus the monadic jc seems OK, but the diadic jc fails. ("x jc y" should give
>the first x elements of y.)

>I tried debugging with dbx. The new array was generated correctly, but
>something went wrong in the printing process. I had trouble following
>what was happening.

>I tried modifying the code to retain the normal main but still define
>10!:0 (essentially as Emmett McLean did in J-lapack interface). I got the
>same problem again.

>Has anyone else had similar problems?  Any help would be much appreciated.

 I suggest running a few programs on your implementation of J. If these
 programs run ok then you should be able to use linkj or properj with
 out problems. Based on your example, it looks like memory is not
 being copied with memcopy.

 Here is a hack which implements ntr in properj, say as lapack 30!:k
 which worked ok for me: (OTOH ntr worked for me too.)

static F2(ntr){A z;I n;
 ASSERT(0==AR(a),EVRANK);       /* a must be scalar                        */
 ASSERT(INT&AT(a),EVDOMAIN);    /* a must be integral                      */
 ASSERT(INT&AT(w),EVDOMAIN);    /* w must be integral                      */
 n=*AV(a);                      /* value of x                              */
 ASSERT(n<=AN(w),EVLENGTH);     /* x<#,y                                   */
 GA(z,INT,n,1,n);               /* allocate space for result               */
 *AS(z)=n;                      /* the shape of z                          */
 memcpy(AV(z),AV(w),4*n);       /* copy into result, 4 bytes per element   */
 R z;

Quote:
}

 See if this works.

Quote:
> I had trouble following what was happening.

 What do you mean? Can you use jpr in dbx? as in print jpr(w) ?

Quote:
>Should I report the problem to Iverson Software Inc.? If so, can they be
>contacted by email?

 To my knowledge not for now. Maybe Eric is available via some
 apl93 address but I don't know what it would be. Encourage
 hime to netify.

Quote:
>There are two distinct parts to LinkJ, one for calling C from J & the other
>for calling J from C. There should be two preprocessor flags to control
>these separately, rather than just LINKJ which enables/disable both
>together.
>--

 Good idea.

 As an aside, I suggest never using linkj at all. linkj aps have numeric
 error messages, can not be ported to the pc to run under windows, and
 results from the functions in Appendix F of IJ have to be cast to A
 types (unless you hack lj.h).

 -emmett



Mon, 29 Jan 1996 17:58:56 GMT  
 Calling C from J
My original post commenced:

: I am having trouble using the LinkJ interface on an SGI unix system.
: I decided to start by trying the example in main.c as discussed in Appendix C
: of Roger Hui's "An Implementation of J".
:
: I modified js.h as follows:
: #define LINKJ 1

I failed to mention fact that I had also set SYS_DOUBLE in js.h as follows:
#define SYS_DOUBLE  1
This turned out to be cause of problem. I had wrongly assumed SYS_DOUBLE
controlled whether real var's were stored as floats or doubles. I do not
know what it is for.

Thanks to Emmett Mclean for reply.
Apologies for wasting your time.
--
Harvey Davies,
CSIRO Division of Atmospheric Research,    Voice: +61 3 586 7574
Private Bag No. 1, Mordialloc,               Fax: +61 3 586 7600



Tue, 30 Jan 1996 13:33:55 GMT  
 Calling C from J
Harvey Davies:
.  I failed to mention fact that I had also set SYS_DOUBLE in js.h as
.  follows:
.  #define SYS_DOUBLE  1

.  This turned out to be cause of problem. I had wrongly assumed
.  SYS_DOUBLE controlled whether real var's were stored as floats or
.  doubles. I do not know what it is for.

$ cd j/src
$ grep SYS_DOUBLE *.[ch]
js.h:#define SYS_DOUBLE              1
jt.h:#if (SYS_DOUBLE)
jt.h:#if (SYS_DOUBLE)
$

Looking in jt.h, SYS_DOUBLE is used in the definition of AV (which
jt.h identifies as a pointer to the first array element).  A closer
examination shows that if SYS_DOUBLE is set, a 1 is added to the AV
offset if the rank of the array is odd.  SYS_DOUBLE is also used in
defining WP (and a little more grepping shows that WP is used to
determine how much memory to allocate for a j array).  In analogy to
AV, a 1 is added to the size of an array (in 'long' words) if the rank
of the array is odd.

Also, I just happen to know that SYS_DOUBLE is for use on machines
which require that double precision floating point numbers be aligned
on an 8-byte boundary.  I also happen to know that J's AV and WP talk
in terms of C's long integers.  And, for the typical implementation of
C on such a machine, AP guarantees that the data segment of all arrays
is aligned on an 8-byte boundary, and WP guarantees that enough memory
is allocated to hold the array, with padding bytes.

I suspect that the linkj headers don't quite deal with SYS_DOUBLE
properly...




Tue, 30 Jan 1996 19:49:08 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. JS-EAI with *JS*-callback

2. js.exception 3279.js

3. to CS: or not to CS: in F-PC assembler

4. Call for programs (CS investigation)

5. NB. gray.js: a J verb that generates a grayscale postscript image from a 2d array

6. lapackTest.js

7. profile.js

8. J script file profile.js

9. JS valueOf() in RB ?

10. JS for functional programming?

11. JS for functional programming?

12. prefs.js question

 

 
Powered by phpBB® Forum Software