How to use math functions in dcompute?

Sobaya sobaya007 at gmail.com
Fri Sep 7 06:45:32 UTC 2018


On Friday, 31 August 2018 at 13:30:10 UTC, Nicholas Wilson wrote:
> On Thursday, 30 August 2018 at 10:34:33 UTC, Sobaya wrote:
>> On Monday, 27 August 2018 at 12:47:45 UTC, Nicholas Wilson 
>> wrote:
>>> On Monday, 27 August 2018 at 09:57:18 UTC, Sobaya wrote:
>>>> On Monday, 27 August 2018 at 09:41:34 UTC, 9il wrote:
>>>>> On Monday, 27 August 2018 at 08:25:14 UTC, Sobaya wrote:
>>>>>> I'm using dcompute(https://github.com/libmir/dcompute).
>>>>>>
>>>>>> In the development, I have got to use math functions such 
>>>>>> as sqrt in @compute function.
>>>>>>
>>>>>> But LDC says "can only call functions from other @compute 
>>>>>> modules in @compute code", so can't I call any math 
>>>>>> functions with dcompute?
>>>>>>
>>>>>> Is there any way to use predefined math functions in 
>>>>>> dcompute?
>>>>>>
>>>>>> Thanks.
>>>>>
>>>>> You may want to try ldc.intrinsics / mir.math.common
>>>>
>>>> Do you mean llvm_sqrt in ldc.intrinsics?
>>>>
>>>> These functions are also not @compute code, so they cause 
>>>> the same error.
>>>
>>> Thanks for bringing this to my attention, will fix soon. In 
>>> the meantime you may declare your own intrinsics in a 
>>> @compute module and it should work. as in
>>>
>>> @compute module mymath;
>>>
>>> pragma(LDC_intrinsic, "llvm.sqrt.f#")
>>>     T llvm_sqrt(T)(T val)
>>>         if (__traits(isFloating, T));
>>>
>>>
>>> This will work if you are targeting CUDA, SPIRV may not like 
>>> it because the backend is less... mature.
>>
>> Thank you for replaying.
>>
>> Surely the definition you told me works for "sqrt".
>> But "cos" and "sin" does not work.
>> The error message is
>>
>> LLVM ERROR: Cannot select: 0xd76ffd8: f32 = fcos 
>> ConstantFP:f32<0.000000e+00>
>>    0xd76ff70: f32 = ConstantFP<0.000000e+00>
>>
>> What's wrong?
>
> SPIR-V or CUDA?
>
> for SPIR-V try
>
> pragma(mangle, "_Z3sinf")
> float sin(float);
> pragma(mangle, "_Z3cosf")
> float cos(float);
>
> more generally see 
> https://github.com/KhronosGroup/SPIR-Tools/wiki/SPIR-2.0-built-in-functions
>
> If this is a problem with CUDA you could try using the NVPTX 
> intrinsics
>
> pragma(LDC_intrinsic, "llvm.nvvm.namegoeshere")
> T namegoeshere(Args a);
>
> If you need to use both SPIR-V and CUDA then see the hackery 
> e.g. 
> https://github.com/libmir/dcompute/blob/master/source/dcompute/std/index.d#L45
>
> LLVM will be released on September 5th I will fix up this 
> shortly after.
>
> Sorry for the alpha state of things right now.
>
> Nic

Sorry for being late for reply.

I'm using CUDA for back-end.

So you mean if required function is "cos",

pragma(LDC_intrinsic, "llvm.nvv.cos")
T cos(T a);

Is it right?

I tried this declaration, but I got an error diffrent from 
previous one:

dcompute.driver.error.DComputeDriverException at ../../.dub/packages/dcompute-master/dcompute/source/dcompute/driver/error.d(172): cast(Status)218
----------------
??:? [0x48739e]
??:? [0x48f8aa]
??:? [0x47942d]
error.d:172 [0x42fe61]
error.d:167 [0x42fdb4]
error.d:187 [0x42feca]
program.d:30 [0x42fc00]
app.d:13 [0x417bea]
??:? [0x47908f]
??:? [0x478f85]
__entrypoint.d:8 [0x4289f4]
??:? __libc_start_main [0x2b85f455282f]
??:? [0x405c08]
Program exited with code 1

My declaration is wrong?

Or fixed LLVM(LDC?) is already released?

How can I use it ?


More information about the Digitalmars-d-learn mailing list