Fortran API

The Fortran API resembles most the C API. To limit portability issues, all MUSCLE Fortran commands are implemented in C but can be called directly from Fortran. It assumes that all function parameters are passed as references, which is the default if using a Fortran variable, or can be forced with the %REF primitive. For instance, %REF(100) passes a reference to the integer 100 to C. The API is as follows:

MUSCLE_Init(integer argc, character*(*) argv)
MUSCLE_Finalize()

MUSCLE_Kernel_Name(character*255 result)
MUSCLE_Get_Property(character*255 name, character*1024 result)

MUSCLE_Will_Stop(logical result)

MUSCLE_Receive(character*255 exit_name, [your_datatype]*65536 result, integer size, muscle_datatype_t type)
MUSCLE_Send(character*255 entrance_name, [your_datatype]*(*) data, integer size, muscle_datatype_t type)

The length of the result from MUSCLE_Kernel_Name and MUSCLE_Get_Property can be found by calling len_trim. In each case, if a character array is expected, it should be NUL-terminated, achieved by concatenating with a char(0):

call MUSCLE_Receive("portIn"//char(0), data, len, MUSCLE_DOUBLE)

If the length is not a variable in your code, you should pass a reference (for example for size 10): %REF(10).

To use the enumeration containing the MUSCLE types, include the following statement in blocks where it is used:

enum, bind(c)
	enumerator :: MUSCLE_DOUBLE, MUSCLE_FLOAT, MUSCLE_INT32, MUSCLE_INT64, MUSCLE_STRING, MUSCLE_BOOLEAN, MUSCLE_RAW
endenum

This ensures that the datatypes are the same as used in the C/C++ code. Due to the difficulty of converting arrays of character arrays to C, instead of the standard MUSCLE_Init, add the following subroutine to your code, that will convert the string to C-compatible strings.

subroutine muscle_fortran_init()
	implicit none
	integer :: argc, i, prevlen, newlen
	character(len=25600) :: argv
	character(len=255) :: arg

	prevlen = 0
	argc = command_argument_count()
	
	do i = 0, argc
        call get_command_argument(i, arg)
		newlen = len_trim(arg)
		argv = argv(1:prevlen) // arg(1:newlen) // char(0)
		prevlen = prevlen + newlen + 1
	end do
    
	call MUSCLE_Init(argc, argv(1:prevlen))
end subroutine muscle_fortran_init

Example Code

sender.F90