Skip to content

Commit 804a286

Browse files
Intermittent commit for GPU STLs
1 parent 0a089ce commit 804a286

2 files changed

Lines changed: 61 additions & 17 deletions

File tree

src/common/m_model.fpp

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,23 @@ contains
479479
is_buffered = .true.
480480
end subroutine s_skip_ignored_lines
481481

482+
!> This function is used to replace the fortran random number
483+
!! generator because the native generator is not compatible being called
484+
!! from GPU routines/functions
485+
function f_model_random_number(seed) result(rval)
486+
487+
! $:GPU_ROUTINE(parallelism='[seq]')
488+
489+
integer, intent(inout) :: seed
490+
real(wp) :: rval
491+
492+
seed = ieor(seed, ishft(seed, 13))
493+
seed = ieor(seed, ishft(seed, -17))
494+
seed = ieor(seed, ishft(seed, 5))
495+
496+
rval = abs(real(seed, wp)) / real(huge(seed), wp)
497+
end function f_model_random_number
498+
482499
!> This procedure, recursively, finds whether a point is inside an octree.
483500
!! @param model Model to search in.
484501
!! @param point Point to test.
@@ -493,44 +510,48 @@ contains
493510
real(wp), dimension(1:3), intent(in) :: point
494511
real(wp), dimension(1:3), intent(in) :: spacing
495512
integer, intent(in) :: spc
513+
real(wp) :: phi, theta
514+
integer :: rand_seed
496515

497516
real(wp) :: fraction
498517

499518
type(t_ray) :: ray
500-
integer :: i, j, nInOrOut, nHits
519+
integer :: i, j, k, nInOrOut, nHits
501520

502521
real(wp), dimension(1:spc, 1:3) :: ray_origins, ray_dirs
503522

504-
! TODO :: The random number generation prohibits GPU compute due to the subroutine not being able to be called in kernels
505-
! This should be swapped out with something that allows GPU compute. I recommend the fibonacci sphere:
506-
! do i = 1, spc
507-
! phi = acos(1.0 - 2.0*(i-1.0)/(spc-1.0))
508-
! theta = pi * (1.0 + sqrt(5.0)) * (i-1.0)
509-
! ray_dirs(i,:) = [cos(theta)*sin(phi), sin(theta)*sin(phi), cos(phi)]
510-
! ray_origins(i,:) = point
511-
! end do
523+
rand_seed = int(point(1) * 73856093_wp) + &
524+
int(point(2) * 19349663_wp) + &
525+
int(point(3) * 83492791_wp)
526+
if (rand_seed == 0) rand_seed = 1
512527

528+
! generate our random collection or rays
513529
do i = 1, spc
514-
call random_number(ray_origins(i, :))
515-
ray_origins(i, :) = point + (ray_origins(i, :) - 0.5_wp)*spacing(:)
516-
517-
call random_number(ray_dirs(i, :))
518-
ray_dirs(i, :) = ray_dirs(i, :) - 0.5_wp
530+
do k = 1, 3
531+
! random jitter in the origin helps us estimate volume fraction instead of only at the cell center
532+
ray_origins(i, k) = point(k) + (f_model_random_number(rand_seed) - 0.5_wp) * spacing(k)
533+
! cast sample rays in all directions
534+
ray_dirs(i, k) = point(k) + f_model_random_number(rand_seed) - 0.5_wp
535+
end do
519536
ray_dirs(i, :) = ray_dirs(i, :)/sqrt(sum(ray_dirs(i, :)*ray_dirs(i, :)))
520537
end do
521538

539+
! ray trace
522540
nInOrOut = 0
523541
do i = 1, spc
524542
ray%o = ray_origins(i, :)
525543
ray%d = ray_dirs(i, :)
526544

527545
nHits = 0
528546
do j = 1, model%ntrs
547+
! count the number of triangles this ray intersects
529548
if (f_intersects_triangle(ray, model%trs(j))) then
530549
nHits = nHits + 1
531550
end if
532551
end do
533552

553+
! if thje ray hits an odd number of triangles on its way out, then
554+
! it must be on the inside of the model
534555
nInOrOut = nInOrOut + mod(nHits, 2)
535556
end do
536557

src/simulation/m_ib_patches.fpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,9 @@ contains
138138

139139
real(wp), dimension(1:4, 1:4) :: transform, transform_n
140140

141-
call random_seed()
142-
143141
do patch_id = 1, num_ibs
144142
if (patch_ib(patch_id)%geometry == 5 .or. patch_ib(patch_id)%geometry == 12) then
145-
allocate (models(patch_id)%model)
143+
@:ALLOCATE(models(patch_id)%model)
146144
print *, " * Reading model: "//trim(patch_ib(patch_id)%model_filepath)
147145

148146
model = f_model_read(patch_ib(patch_id)%model_filepath)
@@ -237,6 +235,30 @@ contains
237235
models(patch_id)%total_vertices = total_vertices
238236
end if
239237

238+
! update allocatables
239+
$:GPU_ENTER_DATA(copyin='[models(patch_id)]')
240+
$:GPU_ENTER_DATA(copyin='[models(patch_id)%model]')
241+
$:GPU_ENTER_DATA(copyin='[models(patch_id)%model%trs]')
242+
$:GPU_ENTER_DATA(copyin='[models(patch_id)%model%ntrs]')
243+
$:GPU_ENTER_DATA(copyin='[models(patch_id)%boundary_v]')
244+
do i = 1, models(patch_id)%model%ntrs
245+
$:GPU_ENTER_DATA(copyin='[models(patch_id)%model%trs(i)]')
246+
end do
247+
if (interpolate) then
248+
$:GPU_ENTER_DATA(copyin='[models(patch_id)%interpolated_boundary_v]')
249+
end if
250+
251+
print *, "Entering GPU loop to read"
252+
253+
! TODO :: REMOVE ME
254+
$:GPU_PARALLEL_LOOP(private='[i]')
255+
do i = 1, 3
256+
print *, "Num Triangles", models(1)%model%ntrs
257+
! print *, "interpolate", models(i)%interpolate
258+
! print *, "Vertices", models(i)%model%trs(1)%v
259+
end do
260+
$:END_GPU_PARALLEL_LOOP()
261+
240262
end if
241263
end do
242264

@@ -995,6 +1017,7 @@ contains
9951017
9961018
! Reading STL boundary vertices and compute the levelset and levelset_norm
9971019
if (eta > patch_ib(patch_id)%model_threshold) then
1020+
print *, eta, i, j
9981021
ib_markers%sf(i, j, 0) = patch_id
9991022
end if
10001023

0 commit comments

Comments
 (0)