LKedward / focal

A modern Fortran abstraction layer for OpenCL

Home Page:https://lkedward.github.io/focal-docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cross-Platform compiling problems

ywgATustcbbs opened this issue · comments

I am trying to compile focal and run your example using vs2019 + intel fortran(2021) on windows. Then I found some problems (while it works fine on ubuntu):

  1. Compilation of focal and 'sum.f90' is OK. But when debuging, a very strange exception was thrown :

    Access violation at 0x0000000000000000.

After tons of investigation, I found it was caused by the using of function "fclErrorHandler()". Replace it with "fclDefaultErrorHandler()" throughout the code will fix this problem.

After reading the source code, I guess you want the user to redirect the default handler to his own one. But I have no idea why the code failed with intel fortran (even with f2018 semantics).

  1. Afer the first problem, I came across a harder one and stuck here.

When debuging, "sumKernel = fclGetProgramKernel(prog,'sum')"(sum.f90 line 31) get errorcode 46.

The exact failure point is "kern%cl_kernel = clCreateKernel(prog%cl_program,C_LOC(c_name),errcode)"(fcl_Setup.f90 line 583). But I do not know why.

By comparing fortran code to my c++ code, It seems that the problem is caused by sum.f90 line 30:

errcode = clBuildProgram(prog%cl_program,0, &
C_NULL_PTR,C_LOC(c_options),C_NULL_FUNPTR,C_NULL_PTR)

line 501, in function :fclCompileProgram_1 () focal_Setup.f90. The 2nd parameter should be the number of total platforms(devices) and the 3rd should be the pointer to the device

//OpenCL source
extern CL_API_ENTRY cl_int CL_API_CALL
clBuildProgram(cl_program program,
cl_uint num_devices,
const cl_device_id * device_list,
const char * options,
void (CL_CALLBACK * pfn_notify)(cl_program program,
void * user_data),
void * user_data) CL_API_SUFFIX__VERSION_1_0;

I use c/c++ most and I'm new to fortran. So I'm not sure if this is the point. Moreover, I do not know how to modify the code to test.

These problems confused me quite a long time. I am using fortran just to integrate opencl into some very old fortran code to improve the performance..

Hi @ywgATustcbbs, thanks for reporting and I'm sorry to hear you've spent a long time struggling with this.
Thanks for letting me know about the issue with the fclErrorHandler() pointer - it should be quite straightforward for me to implement a workaround for ifort along the same lines as you mentioned.

Regarding your second point, could you try un-commenting line 28 in sum.f90 and commenting-out line 29:

focal/examples/sum.f90

Lines 28 to 29 in c28598b

! call fclSourceFromFile('examples/sum.cl',kernelSrc)
call fclGetKernelResource(kernelSrc)

To explain, fclSourceFromFile will load the kernel source from file at runtime whereas fclGetKernelResource will load a kernel source that has been bundled into the executable, see here for more info.
If you're using Visual Studio then I imagine you are not bundling the kernel source into the executable and hence fclGetKernelResource may be silently returning nonsense which is then causing fclGetProgramKernel to throw a CL_INVALID_KERNEL_NAME(-46) error.

Regarding your point about clBuildProgram, this is what the OpenCL standard (1.2) says:

If device_list is NULL value, the program executable is built for all devices associated with program for which a source or binary has been loaded.

Please do let me know how you get on.

@LKedward Thanks, it works after commenting out line 29.

I'm glad you got it working. I've opened a separate issue (#9) for the error handler procedure pointer issue, and so I'll close this one as resolved. Do let me know if you have any other issues, I'm happy to help! As you are new to Fortran, you may also find the Fortran-lang Discourse useful for getting help.