"TAJCHMAN Marc" <marc.tajch...@cea.fr> wrote on 09/20/2009 06:47:25 AM:

> Thanks a lot, Igor.
> 
> Here is a slightly larger example. The X10 main function calls a 
> native C++ function providing a 1D local array (Rail[double]) and gets
> back some scalar value (a norm of the input vector).
> 
> It seems to work well : I used "x10aux::ref<x10::lang::Rail<double> >"
> for the vector parameter and a reference for the result (double &).

Hi, Marc,

You have to be careful to not introduce unspecified behavior.

X10 does not have call-by-reference.  So you've just stepped outside
of X10 semantics.

This in itself is not necessarily a bad thing, but with the compiler
optimizations we may end up doing, the compiler might get confused
and invalidate your code.

I would change this to use a single-element Rail as the return
parameter, and use a pointer in the C++ code, e.g.,

    @Native("c++", "void compute(x10aux::ref<x10::lang::Rail<double> >, 
double, double *); compute(#1, #2, (#3)->raw())")
    public native def compute(val x:Rail[double], val p:Double, var 
r:Rail[double]{self.length==1}): Void;

and then read the value out of r(0).  That way you stay completely
within the semantics of the language.

Hope this helps,
        Igor

> Could you or anyone take a look on the code to see if I used the 
> proper way to write it ?
> 
> Thanks in advance.
> 
> Mark
> 
> 
> // test7.x10
> import x10.compiler.Native;
> 
> public class test7 {
> 
>     @Native("c++", "void compute(x10aux::ref<x10::lang::Rail<double> 
> >, double, double &); compute(#1, #2, #3)")
>     public native def compute(val x:Rail[double], val p:Double, var r:
> Double): Void;
> 
>     public static def main(a: Rail[String]) : void {
> 
>         for(var j:int =0; j<Place.MAX_PLACES; j++)
>             async(here.next(j)) {
>                 var H:test7 = new test7();
>                 Console.OUT.println("Place " + (here.id + 1)
>                                     + " ( of "+Place.MAX_PLACES+" )");
>                 val n : int = 5;
>                 val u : Rail[Double] = Rail.makeVal[Double](n);
>                 for (var i:int = 0; i<n; i++)
>                     u(i) = here.id+i;
> 
>                 val r:Double = 0.0;
> 
>                 H.compute(u, 1.0, r);
>                 Console.OUT.println("r = " + r);
> 
>                 H.compute(u, 2.0, r);
>                 Console.OUT.println("r = " + r);
>             }
>     }
> }
> 
> // compute.cc
> #include "test7.h"
> #include <math.h>
> 
> void compute(x10aux::ref<x10::lang::Rail<double> > u, double p, double 
&r)
> {
>      double s = 0.0;
>      x10::lang::Rail<double>::Iterator i(u);
>      for (; i.hasNext(); )
>          s += pow(i.next(), p);
> 
>      r = pow(s, 1.0/p);
> }

-- 
Igor Peshansky  (note the spelling change!)
IBM T.J. Watson Research Center
XJ: No More Pain for XML's Gain (http://www.research.ibm.com/xj/)
X10: Parallel Productivity and Performance (http://x10.sf.net/)


------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
X10-users mailing list
X10-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/x10-users

Reply via email to