Hi,
I'm writing some FORTRAN code that calls a C library, and having issues when I compile the code with -O3. The code that is breaking is this:
Subroutine create_qsched_tasks(nr_threads) Use, Intrinsic :: ISO_C_BINDING Use cell_list_module Use quicksched Implicit None Integer, Intent(In) :: nr_threads Integer(Kind=C_INT), Dimension(1:number_cells) :: cell_res, sorts Integer(Kind=C_INT) :: i, j ,k, id, p, id2, temp Integer(Kind=C_INT), Target :: data(0:10) Type(C_PTR) :: dat_ptr dat_ptr = C_LOC(data(0)) qsched = f_qsched_create() call qsched_init(qsched,Int(nr_threads, C_INT), 0) do i=1,number_cells cell_res(i) = qsched_addres(qsched, qsched_owner_none, qsched_res_none) print *, i data(0) = i sorts(i) = qsched_addtask(qsched, type_sort, 0, dat_ptr, int(c_sizeof(type_sort), C_INT), cell_list(i)%cell_count) call qsched_addlock(qsched, sorts(i), cell_res(i)) end do
The qsched_addtask function runs some code, but also contains
printf("data is %i, size is %i\n", *(int*)data, data_size);
where data is a void* and is argument 4, and data_size is argument 5. The output from this code is:
1
data is 0, size is 4
2
data is 0, size is 4
3
data is 0, size is 4
However with -O2 I get:
1
data is 1, size is 4
2
data is 2, size is 4
3
data is 3, size is 4
The function itself seems to work fine, as following this code I have another statement:
do i=0, x_cells+1 do j=0, y_cells+1 do k=0, z_cells+1 id = 1+i+(x_cells+2)*(j+(y_cells+2)*k) data(0) = id temp = qsched_addtask(qsched, type_vdw_self, 0, dat_ptr, int(c_sizeof(type_sort),C_INT), cell_list(id)%cell_count) call qsched_addlock(qsched, temp, cell_res(id)) call qsched_addunlock(qsched, sorts(id), temp) ...
which functions completely as expected, and the value stored in data(0) is passed correctly to the C library.
The interoperable function in question is as follows:
Integer(Kind=C_INT) Function qsched_addtask(s, types, flags, data, data_size, cost) BIND(C) Use, Intrinsic :: ISO_C_BINDING Implicit None Type(C_PTR), VALUE :: s Integer(Kind=C_INT), Intent(In), VALUE :: types Integer(Kind=C_INT), Intent(In), VALUE :: flags Type(C_PTR), VALUE :: data Integer(Kind=C_INT), Intent(In), VALUE :: data_size Integer(Kind=C_INT), Intent(In), VALUE :: cost End Function
Am I doing something wrong that means the compiler thinks it can optimise away the value stored in data(0) in the first case?
Thanks
Aidan Chalk