last day (16 days later) » 

18:11
0
Q: is it possible to allocate an unname memory and then associate pointers in fortran?

RainMy question is motivated by the custom fortran datatypes. an example is given as follows: type mytype integer, pointer :: a(:) real, pointer :: b(:) end type type(mytype), pointer :: data(:) ! this MPI routine reserves a memory region call mpi_win_allocate_shared(size,disp_unit,...,base...

The above is all very confusing. Firstly integer, pointer :: a(:) is not an array of pointers to integer, it is a pointer to an array of integers. Secondly in Fortran you can't do pointer arithmetic (thank god!), either with Fortran pointers or type( c_ptr ) - for the later the + operator is not overloaded. Basically you are trying to write C in Fortran, but they are different languages and may express similar ideas in very different ways. What are you really trying to do?
@IanBush, Hi Ian thanks for the comment. It is not confusing actually, you described what just what I wanted, i.e., a(:) is a pointer to an array of integers. Now the reason I wanted pointers is because I want to store a fortran custom data type using MPI_shared, and access it between various processes. I only know how to do it using C pointers but my work requires that I use fortran.
c_f_pointer returns a Fortran pointer as its second argument. As written you have it returning an integer. real or type(mytype), but you might think it a pointer if you had the misunderstanding I alluded to - hence my confusion
you are right about the wrong code I have provide Ian. I have thus updated it, and hopefully it makes sense now. I hope my question still makes sense, i.e., since fortran does not support pointer arithmetic, how should I have the pointers data, data(i)%a and data(i)%b correctly point to a pre-allocated block of memory using MPI
OK, trying to think through what you might be wanting to do replacing all the loops part with simply Call c_f_pointer( baseptr, data, [ n1 ] ) might do what you want if you make your derived type interoperable. But as I'm not sure quite what you want I won't try to answer, at least not yet.
18:11
@IanBush --- here's what I want to do: I need to use MPI to initiate multiple instances of fortran code to perform a numerical calculation. Different MPI processes run independently, except that they all share the same huge data structure. As a result, it makes sense to use MPI_allocate_shared to only create the data structure once, and have other MPI processes directly read from it. However, being a custom-defined type, it is not clear how to instruct other MPI processes to know the data structure. In C this can be achieved via pointer arithmetics. However in fortran it is not clear
Hi Ian, first of all, thanks for your answers! I've been stuck on the problem for past three days, and it would be nice to get some help. Please let me know if the question is clear enough.
18:40
@Rain Haven't used chat before, hope this works ... Anyway you might be able to do it with a combination of C_loc and c_f_pointer. Set up the huge data structure as an array of e.g. integers, use c_loc on the elements with which you want one of your pointers associated, and then use c_f_pointer to turn the c_ptr into a Fortran pointer. All a bit fiddly, but at the moment I can't think of another way.
@Rain Also I think if I were doing this I would allocate the shared memory for each of the pointers I want individually and tie them up in a derived type. This will be easier to read code, IMO, but might have performance implications - if you just set it up once probably not an issue, if you want to dynamically change its size regularly, or send all of it to another node, there might be more problems.
18:57
Thanks foe
thx for the response ! I will look into c_loc
On the issue of sharing , are you recommending that I combine mpi_dataypes and mpi_shared?
It is not entirely clear to me how to combine them, although I know both exists
I could not find material on the internet that covers it
the structure I am working with --- without invoking MPI --- has something like the follows :
type mytype
complex, allocatable: data1(:,:)
real, allocatable :: data2(:)
....
end mytype

type (mytype) , allocatable :: data(:,:)
allocate(data(m,n))
call initialize data_entries(data)
now this piece of data will be fixed after calling the initialize_data_entries subroutine, and only read-from in later calculations.
now I'd like to use MPI to share this particular data across all processes. If I understand correctly, MPI does not trivially recognize fortran derived data types, and to be able to share data between processes, I need some sort of way of "memory map" using pointers
In your view, could you think of a simpler solution?
Another option I am considering, is to do the regular allocation/initialization on MPI rank 0, and use the MPI_win_allocate_dynamic + MPI_win_attach + MPI_type_struct. However I am not knowledgeable enough if these three contain enough information for another process (e.g., MPI rank 1 on the same node) to easily read from attached data.
19:23
@Rain OK, think I have knocked together something that addresses what you want, will post as an answer to the question
19:37
many thanks! I am studying your answer now, but looks like using c_loc will make it work.
is it necessary to use c_int, c_double, instead of the usual fortran integer*4, complex*16 etc? are there differences in how numbers are allocated?
@Rain Yes. NEVER use integer*4. They are not part of standard Fortran. they have never been part of standard Fortran. Using them makes your code non portable. Their use is just bad practice. Don't get into the habit!
oops got it. What about integer(kind=4), real(kind=kind(0.d0))? These are the codes I am working with. If possible I probably don't want to modify the code significantly
@Rain c_int etc. are designed explicitly to map onto c datatypes - Integer( c_int ) will map onto int etc.
@Rain Their use might not be required here, it is a slightly unusual case, but if you do use them interacting with Type( c_ptr ) become much more easy - technically variables with those kinds are interoperable with C, and that is a requirement for a lot of the uses of c_ptr. It gets more complicated than I have time to explain here - suggest you look at a good book like Metcalf, Reid and Cohen "Modern Fortran"
Integer( 4 ) are standard but bad practice - a compiler is not required to support a kind of 4, and note the 4 has nothing to do with the bytes required to represent the datatype
The iso_fortran_env method mentioned in the comments is the way I would recommend nowadays
got it, thanks!
 
3 hours later…
22:31
Hi Ian, one last question (I promise!): since fortran array indexing is non-standard, is there a way of specifying the index range (f_ptr(start:end) ) when calling c_to_f_pointer(c_ptr,f_ptr, []) ?
@IanBush

  last day (16 days later) »