Re: [julia-users] Embedding Julia in C++ - Determining returned array types

2016-10-19 Thread Isaiah Norton
e.g. http://half.sourceforge.net/

But if you aren't sure you need to support this, then just throw an error
(Float16 is relatively new and AFAICT only useful in specialized
applications because of the precision limit, which is why support is rare)

If you are sure you (a) need it but (b) only need to up- and down-convert,
see the routines in `base/float.jl`. They should be straightforward to port
to C++.

On Wed, Oct 19, 2016 at 1:00 PM, Yichao Yu  wrote:

>
>
> On Wed, Oct 19, 2016 at 12:54 PM, Kyle Kotowick  wrote:
>
>> Aha, that fixed it!
>>
>> I'm running into one issue though. What do I do with the data when it's a
>> "Float16" type? C++ has no way to represent a 16-bit float, so I'm having
>> difficulty converting it to a regular 32-bit float.
>>
>
> Find a compiler / library that supports it? ;-p
>
> I'm only half joking. You can always just keep a pointer to the value or
> store it in a int16_t as long as you don't need to do any operation on it.
> If you want to operate on them in C++ natively you would have to find a
> C/C++ compiler/library (or write the necessary operations yourself) that
> supports __fp16.
>
>
>>
>> jl_value_t *ret = jl_eval_string(code_string);
>> jl_array_t *ret_array = (jl_array_t*)ret;
>>
>> if (jl_array_eltype(ret) == jl_float16_type) {
>>
>>  /* now I need to convert the 'ret_array' to a C++ array of floats, but
>> how do I do that when they're given in Float16? */
>>
>> }
>>
>>
>> On Tuesday, 18 October 2016 20:57:10 UTC-4, Isaiah wrote:
>>>
>>> The issue here is that `jl_array_eltype` is already returning a type.
>>>
>>> `jl_typeis(v, t)` becomes `jl_typeof(v) == t`, so your checks become:
>>>
>>> jl_typeof(array_type) == jl_int64_type
>>>
>>> But
>>>
>>> jl_typeof(array_type) -> DataType
>>>
>>> Instead, either do the equality check directly:
>>>
>>>   array_type == jl_int64_type
>>>
>>> Or use the exported API only (jlapi.c):
>>>
>>>   strcmp(jl_typename_str(array_type), jl_typename_str(jl_int64_type))
>>>
>>> On Tue, Oct 18, 2016 at 7:52 PM, Kyle Kotowick 
>>> wrote:
>>>
 I apologize for the formatting, that should be:

 jl_value_t *ret = jl_eval_string(code_string);
 void* array_type = jl_array_eltype(ret);
 jl_array_t *ret_array = (jl_array_t*)ret;

 if (jl_typeis(array_type, jl_int64_type)) {
   long *data = (long*) jl_array_data(ret_array);
 }
 else if (jl_typeis(array_type, jl_float64_type)) {
   double *data = (double*) jl_array_data(ret_array);
 }


 And the issue is that even if it is an Int64 or Float64, neither of
 those IF statements will return true.



 On Sunday, 16 October 2016 21:36:03 UTC-4, Kyle Kotowick wrote:
>
> Awesome, thanks. Could you show how to use it in a minimal code
> example? Here's what I'm currently trying, but it does not appear to be
> working:
>
> jl_value_t *ret = jl_eval_string(code_string);
> void* array_type = jl_array_eltype(ret);
> jl_array_t *ret_array = (jl_array_t*)ret;
>
>
>
> if (jl_typeis(array_type, jl_int64_type)) {
>  long *data = (long*) jl_array_data(ret_array);
> }
> else if (jl_typeis(array_type, jl_float64_type)) { double *data = (
> double*) jl_array_data(ret_array);
> }
>
>
> On Friday, 14 October 2016 20:45:18 UTC-4, Isaiah wrote:
>>
>> On Fri, Oct 14, 2016 at 2:28 PM, Kyle Kotowick 
>> wrote:
>>>
>>>
>>> After determining that an array was returned, how would you
>>> determine what the inner type of the array is (i.e. the type of the 
>>> objects
>>> it contains)?
>>>
>>
>> `jl_array_eltype`
>>
>>
>>>
>>> And furthermore, if it returns an array of type "Any", would there
>>> be any way to tell what the type is of any arbitrary element in that 
>>> array?
>>>
>>
>> `jl_typeof`, after retrieving the element (which will be boxed)
>>
>>
>>>
>>> Thanks!
>>>
>>
>>
>>>
>


Re: [julia-users] Embedding Julia in C++ - Determining returned array types

2016-10-19 Thread Stefan Karpinski
You can pull the significant bits and the exponent values out of a Float16
and then use those to compute the value as a Float32:
https://en.wikipedia.org/wiki/Half-precision_floating-point_format

On Wed, Oct 19, 2016 at 1:00 PM, Yichao Yu  wrote:

>
>
> On Wed, Oct 19, 2016 at 12:54 PM, Kyle Kotowick  wrote:
>
>> Aha, that fixed it!
>>
>> I'm running into one issue though. What do I do with the data when it's a
>> "Float16" type? C++ has no way to represent a 16-bit float, so I'm having
>> difficulty converting it to a regular 32-bit float.
>>
>
> Find a compiler / library that supports it? ;-p
>
> I'm only half joking. You can always just keep a pointer to the value or
> store it in a int16_t as long as you don't need to do any operation on it.
> If you want to operate on them in C++ natively you would have to find a
> C/C++ compiler/library (or write the necessary operations yourself) that
> supports __fp16.
>
>
>>
>> jl_value_t *ret = jl_eval_string(code_string);
>> jl_array_t *ret_array = (jl_array_t*)ret;
>>
>> if (jl_array_eltype(ret) == jl_float16_type) {
>>
>>  /* now I need to convert the 'ret_array' to a C++ array of floats, but
>> how do I do that when they're given in Float16? */
>>
>> }
>>
>>
>> On Tuesday, 18 October 2016 20:57:10 UTC-4, Isaiah wrote:
>>>
>>> The issue here is that `jl_array_eltype` is already returning a type.
>>>
>>> `jl_typeis(v, t)` becomes `jl_typeof(v) == t`, so your checks become:
>>>
>>> jl_typeof(array_type) == jl_int64_type
>>>
>>> But
>>>
>>> jl_typeof(array_type) -> DataType
>>>
>>> Instead, either do the equality check directly:
>>>
>>>   array_type == jl_int64_type
>>>
>>> Or use the exported API only (jlapi.c):
>>>
>>>   strcmp(jl_typename_str(array_type), jl_typename_str(jl_int64_type))
>>>
>>> On Tue, Oct 18, 2016 at 7:52 PM, Kyle Kotowick 
>>> wrote:
>>>
 I apologize for the formatting, that should be:

 jl_value_t *ret = jl_eval_string(code_string);
 void* array_type = jl_array_eltype(ret);
 jl_array_t *ret_array = (jl_array_t*)ret;

 if (jl_typeis(array_type, jl_int64_type)) {
   long *data = (long*) jl_array_data(ret_array);
 }
 else if (jl_typeis(array_type, jl_float64_type)) {
   double *data = (double*) jl_array_data(ret_array);
 }


 And the issue is that even if it is an Int64 or Float64, neither of
 those IF statements will return true.



 On Sunday, 16 October 2016 21:36:03 UTC-4, Kyle Kotowick wrote:
>
> Awesome, thanks. Could you show how to use it in a minimal code
> example? Here's what I'm currently trying, but it does not appear to be
> working:
>
> jl_value_t *ret = jl_eval_string(code_string);
> void* array_type = jl_array_eltype(ret);
> jl_array_t *ret_array = (jl_array_t*)ret;
>
>
>
> if (jl_typeis(array_type, jl_int64_type)) {
>  long *data = (long*) jl_array_data(ret_array);
> }
> else if (jl_typeis(array_type, jl_float64_type)) { double *data = (
> double*) jl_array_data(ret_array);
> }
>
>
> On Friday, 14 October 2016 20:45:18 UTC-4, Isaiah wrote:
>>
>> On Fri, Oct 14, 2016 at 2:28 PM, Kyle Kotowick 
>> wrote:
>>>
>>>
>>> After determining that an array was returned, how would you
>>> determine what the inner type of the array is (i.e. the type of the 
>>> objects
>>> it contains)?
>>>
>>
>> `jl_array_eltype`
>>
>>
>>>
>>> And furthermore, if it returns an array of type "Any", would there
>>> be any way to tell what the type is of any arbitrary element in that 
>>> array?
>>>
>>
>> `jl_typeof`, after retrieving the element (which will be boxed)
>>
>>
>>>
>>> Thanks!
>>>
>>
>>
>>>
>


Re: [julia-users] Embedding Julia in C++ - Determining returned array types

2016-10-19 Thread Yichao Yu
On Wed, Oct 19, 2016 at 12:54 PM, Kyle Kotowick  wrote:

> Aha, that fixed it!
>
> I'm running into one issue though. What do I do with the data when it's a
> "Float16" type? C++ has no way to represent a 16-bit float, so I'm having
> difficulty converting it to a regular 32-bit float.
>

Find a compiler / library that supports it? ;-p

I'm only half joking. You can always just keep a pointer to the value or
store it in a int16_t as long as you don't need to do any operation on it.
If you want to operate on them in C++ natively you would have to find a
C/C++ compiler/library (or write the necessary operations yourself) that
supports __fp16.


>
> jl_value_t *ret = jl_eval_string(code_string);
> jl_array_t *ret_array = (jl_array_t*)ret;
>
> if (jl_array_eltype(ret) == jl_float16_type) {
>
>  /* now I need to convert the 'ret_array' to a C++ array of floats, but
> how do I do that when they're given in Float16? */
>
> }
>
>
> On Tuesday, 18 October 2016 20:57:10 UTC-4, Isaiah wrote:
>>
>> The issue here is that `jl_array_eltype` is already returning a type.
>>
>> `jl_typeis(v, t)` becomes `jl_typeof(v) == t`, so your checks become:
>>
>> jl_typeof(array_type) == jl_int64_type
>>
>> But
>>
>> jl_typeof(array_type) -> DataType
>>
>> Instead, either do the equality check directly:
>>
>>   array_type == jl_int64_type
>>
>> Or use the exported API only (jlapi.c):
>>
>>   strcmp(jl_typename_str(array_type), jl_typename_str(jl_int64_type))
>>
>> On Tue, Oct 18, 2016 at 7:52 PM, Kyle Kotowick  wrote:
>>
>>> I apologize for the formatting, that should be:
>>>
>>> jl_value_t *ret = jl_eval_string(code_string);
>>> void* array_type = jl_array_eltype(ret);
>>> jl_array_t *ret_array = (jl_array_t*)ret;
>>>
>>> if (jl_typeis(array_type, jl_int64_type)) {
>>>   long *data = (long*) jl_array_data(ret_array);
>>> }
>>> else if (jl_typeis(array_type, jl_float64_type)) {
>>>   double *data = (double*) jl_array_data(ret_array);
>>> }
>>>
>>>
>>> And the issue is that even if it is an Int64 or Float64, neither of
>>> those IF statements will return true.
>>>
>>>
>>>
>>> On Sunday, 16 October 2016 21:36:03 UTC-4, Kyle Kotowick wrote:

 Awesome, thanks. Could you show how to use it in a minimal code
 example? Here's what I'm currently trying, but it does not appear to be
 working:

 jl_value_t *ret = jl_eval_string(code_string);
 void* array_type = jl_array_eltype(ret);
 jl_array_t *ret_array = (jl_array_t*)ret;



 if (jl_typeis(array_type, jl_int64_type)) {
  long *data = (long*) jl_array_data(ret_array);
 }
 else if (jl_typeis(array_type, jl_float64_type)) { double *data = (
 double*) jl_array_data(ret_array);
 }


 On Friday, 14 October 2016 20:45:18 UTC-4, Isaiah wrote:
>
> On Fri, Oct 14, 2016 at 2:28 PM, Kyle Kotowick 
> wrote:
>>
>>
>> After determining that an array was returned, how would you determine
>> what the inner type of the array is (i.e. the type of the objects it
>> contains)?
>>
>
> `jl_array_eltype`
>
>
>>
>> And furthermore, if it returns an array of type "Any", would there be
>> any way to tell what the type is of any arbitrary element in that array?
>>
>
> `jl_typeof`, after retrieving the element (which will be boxed)
>
>
>>
>> Thanks!
>>
>
>
>>


Re: [julia-users] Embedding Julia in C++ - Determining returned array types

2016-10-19 Thread Kyle Kotowick
Aha, that fixed it!

I'm running into one issue though. What do I do with the data when it's a 
"Float16" type? C++ has no way to represent a 16-bit float, so I'm having 
difficulty converting it to a regular 32-bit float.

jl_value_t *ret = jl_eval_string(code_string);
jl_array_t *ret_array = (jl_array_t*)ret;

if (jl_array_eltype(ret) == jl_float16_type) {

 /* now I need to convert the 'ret_array' to a C++ array of floats, but how 
do I do that when they're given in Float16? */

}


On Tuesday, 18 October 2016 20:57:10 UTC-4, Isaiah wrote:
>
> The issue here is that `jl_array_eltype` is already returning a type.
>
> `jl_typeis(v, t)` becomes `jl_typeof(v) == t`, so your checks become:
>
> jl_typeof(array_type) == jl_int64_type
>
> But
>
> jl_typeof(array_type) -> DataType
>
> Instead, either do the equality check directly:
>
>   array_type == jl_int64_type
>
> Or use the exported API only (jlapi.c):
>
>   strcmp(jl_typename_str(array_type), jl_typename_str(jl_int64_type))
>
> On Tue, Oct 18, 2016 at 7:52 PM, Kyle Kotowick  > wrote:
>
>> I apologize for the formatting, that should be:
>>
>> jl_value_t *ret = jl_eval_string(code_string);
>> void* array_type = jl_array_eltype(ret);
>> jl_array_t *ret_array = (jl_array_t*)ret;
>>
>> if (jl_typeis(array_type, jl_int64_type)) {
>>   long *data = (long*) jl_array_data(ret_array);
>> }
>> else if (jl_typeis(array_type, jl_float64_type)) { 
>>   double *data = (double*) jl_array_data(ret_array);
>> }
>>
>>
>> And the issue is that even if it is an Int64 or Float64, neither of those 
>> IF statements will return true.
>>
>>
>>
>> On Sunday, 16 October 2016 21:36:03 UTC-4, Kyle Kotowick wrote:
>>>
>>> Awesome, thanks. Could you show how to use it in a minimal code example? 
>>> Here's what I'm currently trying, but it does not appear to be working:
>>>
>>> jl_value_t *ret = jl_eval_string(code_string);
>>> void* array_type = jl_array_eltype(ret);
>>> jl_array_t *ret_array = (jl_array_t*)ret;
>>>
>>>
>>>
>>> if (jl_typeis(array_type, jl_int64_type)) {
>>>  long *data = (long*) jl_array_data(ret_array);
>>> }
>>> else if (jl_typeis(array_type, jl_float64_type)) { double *data = (
>>> double*) jl_array_data(ret_array);
>>> }
>>>
>>>
>>> On Friday, 14 October 2016 20:45:18 UTC-4, Isaiah wrote:

 On Fri, Oct 14, 2016 at 2:28 PM, Kyle Kotowick  
 wrote:
>
>
> After determining that an array was returned, how would you determine 
> what the inner type of the array is (i.e. the type of the objects it 
> contains)?
>

 `jl_array_eltype`
  

>
> And furthermore, if it returns an array of type "Any", would there be 
> any way to tell what the type is of any arbitrary element in that array?
>

 `jl_typeof`, after retrieving the element (which will be boxed)
  

>
> Thanks!
>


>

Re: [julia-users] Embedding Julia in C++ - Determining returned array types

2016-10-18 Thread Yichao Yu
On Tue, Oct 18, 2016 at 8:57 PM, Isaiah Norton 
wrote:

> The issue here is that `jl_array_eltype` is already returning a type.
>
> `jl_typeis(v, t)` becomes `jl_typeof(v) == t`, so your checks become:
>
> jl_typeof(array_type) == jl_int64_type
>
> But
>
> jl_typeof(array_type) -> DataType
>
> Instead, either do the equality check directly:
>
>   array_type == jl_int64_type
>
> Or use the exported API only (jlapi.c):
>
>   strcmp(jl_typename_str(array_type), jl_typename_str(jl_int64_type))
>

Just to be clear, this does not guarantee that the two types are equal.
Directly comparing the pointer for types is also "exported API".


>
> On Tue, Oct 18, 2016 at 7:52 PM, Kyle Kotowick  wrote:
>
>> I apologize for the formatting, that should be:
>>
>> jl_value_t *ret = jl_eval_string(code_string);
>> void* array_type = jl_array_eltype(ret);
>> jl_array_t *ret_array = (jl_array_t*)ret;
>>
>> if (jl_typeis(array_type, jl_int64_type)) {
>>   long *data = (long*) jl_array_data(ret_array);
>> }
>> else if (jl_typeis(array_type, jl_float64_type)) {
>>   double *data = (double*) jl_array_data(ret_array);
>> }
>>
>>
>> And the issue is that even if it is an Int64 or Float64, neither of those
>> IF statements will return true.
>>
>>
>>
>> On Sunday, 16 October 2016 21:36:03 UTC-4, Kyle Kotowick wrote:
>>>
>>> Awesome, thanks. Could you show how to use it in a minimal code example?
>>> Here's what I'm currently trying, but it does not appear to be working:
>>>
>>> jl_value_t *ret = jl_eval_string(code_string);
>>> void* array_type = jl_array_eltype(ret);
>>> jl_array_t *ret_array = (jl_array_t*)ret;
>>>
>>>
>>>
>>> if (jl_typeis(array_type, jl_int64_type)) {
>>>  long *data = (long*) jl_array_data(ret_array);
>>> }
>>> else if (jl_typeis(array_type, jl_float64_type)) { double *data = (
>>> double*) jl_array_data(ret_array);
>>> }
>>>
>>>
>>> On Friday, 14 October 2016 20:45:18 UTC-4, Isaiah wrote:

 On Fri, Oct 14, 2016 at 2:28 PM, Kyle Kotowick 
 wrote:
>
>
> After determining that an array was returned, how would you determine
> what the inner type of the array is (i.e. the type of the objects it
> contains)?
>

 `jl_array_eltype`


>
> And furthermore, if it returns an array of type "Any", would there be
> any way to tell what the type is of any arbitrary element in that array?
>

 `jl_typeof`, after retrieving the element (which will be boxed)


>
> Thanks!
>


>


Re: [julia-users] Embedding Julia in C++ - Determining returned array types

2016-10-18 Thread Isaiah Norton
The issue here is that `jl_array_eltype` is already returning a type.

`jl_typeis(v, t)` becomes `jl_typeof(v) == t`, so your checks become:

jl_typeof(array_type) == jl_int64_type

But

jl_typeof(array_type) -> DataType

Instead, either do the equality check directly:

  array_type == jl_int64_type

Or use the exported API only (jlapi.c):

  strcmp(jl_typename_str(array_type), jl_typename_str(jl_int64_type))

On Tue, Oct 18, 2016 at 7:52 PM, Kyle Kotowick  wrote:

> I apologize for the formatting, that should be:
>
> jl_value_t *ret = jl_eval_string(code_string);
> void* array_type = jl_array_eltype(ret);
> jl_array_t *ret_array = (jl_array_t*)ret;
>
> if (jl_typeis(array_type, jl_int64_type)) {
>   long *data = (long*) jl_array_data(ret_array);
> }
> else if (jl_typeis(array_type, jl_float64_type)) {
>   double *data = (double*) jl_array_data(ret_array);
> }
>
>
> And the issue is that even if it is an Int64 or Float64, neither of those
> IF statements will return true.
>
>
>
> On Sunday, 16 October 2016 21:36:03 UTC-4, Kyle Kotowick wrote:
>>
>> Awesome, thanks. Could you show how to use it in a minimal code example?
>> Here's what I'm currently trying, but it does not appear to be working:
>>
>> jl_value_t *ret = jl_eval_string(code_string);
>> void* array_type = jl_array_eltype(ret);
>> jl_array_t *ret_array = (jl_array_t*)ret;
>>
>>
>>
>> if (jl_typeis(array_type, jl_int64_type)) {
>>  long *data = (long*) jl_array_data(ret_array);
>> }
>> else if (jl_typeis(array_type, jl_float64_type)) { double *data = (double
>> *) jl_array_data(ret_array);
>> }
>>
>>
>> On Friday, 14 October 2016 20:45:18 UTC-4, Isaiah wrote:
>>>
>>> On Fri, Oct 14, 2016 at 2:28 PM, Kyle Kotowick 
>>> wrote:


 After determining that an array was returned, how would you determine
 what the inner type of the array is (i.e. the type of the objects it
 contains)?

>>>
>>> `jl_array_eltype`
>>>
>>>

 And furthermore, if it returns an array of type "Any", would there be
 any way to tell what the type is of any arbitrary element in that array?

>>>
>>> `jl_typeof`, after retrieving the element (which will be boxed)
>>>
>>>

 Thanks!

>>>
>>>


Re: [julia-users] Embedding Julia in C++ - Determining returned array types

2016-10-18 Thread Kyle Kotowick
I apologize for the formatting, that should be:

jl_value_t *ret = jl_eval_string(code_string);
void* array_type = jl_array_eltype(ret);
jl_array_t *ret_array = (jl_array_t*)ret;

if (jl_typeis(array_type, jl_int64_type)) {
  long *data = (long*) jl_array_data(ret_array);
}
else if (jl_typeis(array_type, jl_float64_type)) { 
  double *data = (double*) jl_array_data(ret_array);
}


And the issue is that even if it is an Int64 or Float64, neither of those 
IF statements will return true.


On Sunday, 16 October 2016 21:36:03 UTC-4, Kyle Kotowick wrote:
>
> Awesome, thanks. Could you show how to use it in a minimal code example? 
> Here's what I'm currently trying, but it does not appear to be working:
>
> jl_value_t *ret = jl_eval_string(code_string);
> void* array_type = jl_array_eltype(ret);
> jl_array_t *ret_array = (jl_array_t*)ret;
>
>
>
> if (jl_typeis(array_type, jl_int64_type)) {
>  long *data = (long*) jl_array_data(ret_array);
> }
> else if (jl_typeis(array_type, jl_float64_type)) { double *data = (double
> *) jl_array_data(ret_array);
> }
>
>
> On Friday, 14 October 2016 20:45:18 UTC-4, Isaiah wrote:
>>
>> On Fri, Oct 14, 2016 at 2:28 PM, Kyle Kotowick  wrote:
>>>
>>>
>>> After determining that an array was returned, how would you determine 
>>> what the inner type of the array is (i.e. the type of the objects it 
>>> contains)?
>>>
>>
>> `jl_array_eltype`
>>  
>>
>>>
>>> And furthermore, if it returns an array of type "Any", would there be 
>>> any way to tell what the type is of any arbitrary element in that array?
>>>
>>
>> `jl_typeof`, after retrieving the element (which will be boxed)
>>  
>>
>>>
>>> Thanks!
>>>
>>
>>

Re: [julia-users] Embedding Julia in C++ - Determining returned array types

2016-10-15 Thread Bart Janssens
In addition to this, CxxWrap.jl has some additional convenience classes to
work with Julia arrays from C++, see
https://github.com/JuliaInterop/CxxWrap.jl#working-with-arrays

Cheers,

Bart

On Sat, Oct 15, 2016 at 2:45 AM Isaiah Norton 
wrote:

> On Fri, Oct 14, 2016 at 2:28 PM, Kyle Kotowick  wrote:
>
>
> After determining that an array was returned, how would you determine what
> the inner type of the array is (i.e. the type of the objects it contains)?
>
>
> `jl_array_eltype`
>
>
>
> And furthermore, if it returns an array of type "Any", would there be any
> way to tell what the type is of any arbitrary element in that array?
>
>
> `jl_typeof`, after retrieving the element (which will be boxed)
>
>
>
> Thanks!
>
>
>


Re: [julia-users] Embedding Julia in C++ - Determining returned array types

2016-10-14 Thread Isaiah Norton
On Fri, Oct 14, 2016 at 2:28 PM, Kyle Kotowick  wrote:
>
>
> After determining that an array was returned, how would you determine what
> the inner type of the array is (i.e. the type of the objects it contains)?
>

`jl_array_eltype`


>
> And furthermore, if it returns an array of type "Any", would there be any
> way to tell what the type is of any arbitrary element in that array?
>

`jl_typeof`, after retrieving the element (which will be boxed)


>
> Thanks!
>