1

I am using an external fortran library that returns a large array of data with the typeREAL(kind=8). However, I need to write the data out as REAL(kind=4).

What is the best way to convert large arrays of data to a different KIND type?

I am naively just doing this at the moment:

REAL(KIND=8), ALLOCATABLE :: data64(:,:)
REAL(KIND=4), ALLOCATABLE :: data32(:,:)

# allocate arrays and call external function
CALL some_external_function(data64)

# Convert to 32bit using simple assignment
data32 = data64

# Write out 32bit data....
WRITE(UOUT) data32

However, this approach requires me to allocate two large arrays instead of one and do a lot of copying of data - seems a bit inefficient with both respect to memory and processing...

Is there a smarter way?

BTW: The sizes of the matrices are not necessarily huge - they vary but probably in the order of around 1000 x 1000 (but this will be occuring 100s of times in a loop). Not a problem for modern machines, but as one of the comments suggests, I am looking for an elegant solution that is also efficient! Doing the assignement just seems.....lazy and inelagant?

ccbunney
  • 2,282
  • 4
  • 26
  • 42
  • @HighPerformanceMark: I am writing out an unformatted (binary) file; it needs to be KIND=4 as other programs expect the data in this format. – ccbunney Mar 04 '16 at 13:28
  • IF you are doing `I/O` the speed for the conversion wouldn't matter as it is going to be orders of magnitude faster then disk writing. Maybe you want the most _elegant_ solution instead of the most _efficient_. – John Alexiou Mar 04 '16 at 13:47
  • What about converting one row at a time and then appending that row in the file. – John Alexiou Mar 04 '16 at 13:49
  • Oh, btw, what is approximately the size of these matrices? – John Alexiou Mar 04 '16 at 14:07
  • @ja72 - yes, I suppose I want the elegant solution! Have edited my post. – ccbunney Mar 04 '16 at 14:42
  • 1
    Just my usual rant why `kind=4` and `kind=8` are ugly [code smell](https://en.wikipedia.org/wiki/Code_smell): http://stackoverflow.com/questions/3170239/fortran-integer4-vs-integer4-vs-integerkind-4?lq=1. Heck it is even longer than `real(sp)` and `real(dp)`, I simply don't get why would anybody continue to use them. – Vladimir F Героям слава Mar 04 '16 at 14:58
  • @VladimirF - I had not realised that `kind=x` types were not portable. Code smell indeed.... – ccbunney Mar 04 '16 at 15:18

1 Answers1

2

Here's elegant for you

write(uout) real(data64,kind(data32))
High Performance Mark
  • 77,191
  • 7
  • 105
  • 161