Quantcast
Channel: Fortran
Viewing all articles
Browse latest Browse all 3108

Casting a dynamic 1D array into a dynamic 2D array without copying data

$
0
0

Hi All,

I have the following problem. I want to cast a 1D array into column of 2D array in a loop. At 1st iteration the size of this 2D array is same as the 1D array. So. There should be no problem to cast it. At 2nd iteration, 1D array is going to have different elements. I want to cast it to 2nd column of 2D array.

This procedure continues till the norm of elements of 2D array is less than tolerance and it stops there. There,  I can not guess the number of columns of 2D array to allocate it before starting calculation...

here are three codes I have tried. None of them work for me :

program ptrtest

 real, pointer, CONTIGUOUS :: Mr(:)
 real, pointer, CONTIGUOUS :: Mp(:,:)
 real, DIMENSION(9) ::abc


 integer :: n = 2
 iter=3

 Do i=1,iter

 alpha2 = 2
 allocate(Mr(n**2))
 abc= 42

 Mr(1:n**2) = 0.5 * abc(1:n**2)
 write(*,*) 'Mr='
 write(*,555) Mr

 Mr(1:n**2) => Mp(1:n**2,1:1)

 WRITE(*,*) 'Mp='
 WRITE(*,555) Mp

 end do

 555  FORMAT(F12.4,1X)


 end program ptrtest

----------------------------------------------------------------------------------------------------

 

program ptrtest

 real, pointer, CONTIGUOUS :: Mr(:)
 real, pointer, CONTIGUOUS :: Mp(:,:)
 real, DIMENSION(9) ::abc


 integer :: n = 2
 iter=3

 Do i=1,iter

 alpha2 = 2
 allocate(Mr(n**2))
 abc= 42

 Mr(1:n**2) = 0.5 * abc(1:n**2)
 write(*,*) 'Mr='
 write(*,555) Mr

 do j = 1, iter
    Mp(:, j) = Mr
 end do

 WRITE(*,*) 'Mp='
 WRITE(*,555) Mp

 end do

 555  FORMAT(F12.4,1X)


 end program ptrtest

 

----------------------------------------------------------------------------------------------------

program ptrtest
   
   real :: start, finish
   real, pointer :: Mr(:)
   real, pointer,CONTIGUOUS :: Mp(:,:)
   real, DIMENSION(4) ::abc

 
   integer :: n = 2
   
   
   call cpu_time(start)
   iter=2
   alpha2 = 2
   
   Do i=1,iter
   
   allocate(Mp(n**2,i))
   allocate(Mr(n**2))

   data abc/1,2,3,4/
   
   Mr(1:n**2) = 0.5 * abc(1:4)
   write(*,*) 'Mr='
   write(*,555) Mr
   

   Mp = TRANSFER (Mr,1.0)

      
   WRITE(*,*) 'Mp='
   WRITE(*,555) Mp
   
   end do
     
555 FORMAT(F12.4,1X)
   call cpu_time(finish)
   print '("Time = ",f10.7," seconds.")', finish-start
end program ptrtest

 

 

 


Viewing all articles
Browse latest Browse all 3108

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>