This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_target_update_devices.F90
185 lines (154 loc) · 5.81 KB
/
test_target_update_devices.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
!===--- test_target_update_devices.F90 --------------------------------------===//
!
! OpenMP API Version 4.5 Nov 2015
!
!
! This test checks if the target update directive works on different devices.
! We check two different variants.
! 1. setting up the default device with the API call omp_set_default_device()
! 2. using the device clause of the target update directive.
!
! Testing metodology uses an array that gets mapped into the device at first
! through target enter data. Then on each iteration we update the array in one
! device, create a compute region in that device, and then update it back
! We also record that the compute region is not executed in the host
! with the omp_is_initial_device() API call. Unfortunately 4.5 has no device
! number API call.
!
!===-------------------------------------------------------------------------===//
#include "ompvv.F90"
#define N 1024
PROGRAM target_update_devices
USE iso_fortran_env
USE ompvv_lib
USE omp_lib
implicit none
OMPVV_TEST_VERBOSE(test_set_default_dev() .ne. 0)
OMPVV_TEST_VERBOSE(test_device() .ne. 0)
OMPVV_REPORT_AND_RETURN()
CONTAINS
INTEGER FUNCTION test_set_default_dev()
INTEGER :: num_dev, def_dev, summation, errors, i, dev
LOGICAL, DIMENSION(0:5000) :: isHost ! Arbitrary number greater than the num
INTEGER, DIMENSION(N) :: h_matrix
CHARACTER(len = 400) :: numDevMsg, defDevMsg, initDevMsg, resultsMsg
! Initialize vars
errors = 0
summation = 0
OMPVV_INFOMSG("test_set_default_dev()")
! Get number of devices
num_dev = omp_get_num_devices()
WRITE(numDevMsg, *) "num_devices:", num_dev
OMPVV_INFOMSG(numDevMsg)
def_dev = omp_get_default_device()
WRITE(initDevMsg, *) "initial device is:", omp_get_initial_device()
OMPVV_INFOMSG(initDevMsg)
WRITE(defDevMsg, *) "default device is:", def_dev
OMPVV_INFOMSG(defDevMsg)
! Mapping the array to all of the devices
DO dev = 0, num_dev - 1
CALL omp_set_default_device(dev)
!$omp target enter data map (alloc: h_matrix)
END DO
! Initialize the array
DO i = 1, N
h_matrix(i) = 0
END DO
! Each device gets updated with the current array version,
! one gets added to each element in the array, and then
! the host gets the updated version
DO dev = 0, num_dev - 1
CALL omp_set_default_device(dev)
!$omp target update to(h_matrix)
!$omp target map(alloc: h_matrix) map(tofrom: isHost(dev))
isHost(dev) = omp_is_initial_device()
DO i = 1, N
h_matrix(i) = h_matrix(i) + 1
END DO
!$omp end target
!$omp target update from(h_matrix)
END DO
! Unmap the matrix
DO dev = 0, num_dev - 1
CALL omp_set_default_device(dev)
!$omp target exit data map(delete: h_matrix)
END DO
! Checking results
DO dev = 0, num_dev - 1
IF (isHost(dev) .eqv. .TRUE.) THEN
WRITE(resultsMsg, *) "device",dev,"ran on the host"
OMPVV_INFOMSG(resultsMsg)
ELSE
WRITE(resultsMsg, *) "device",dev,"ran on the device"
OMPVV_INFOMSG(resultsMsg)
END IF
END DO
! Checking results
DO i = 1, N
summation = summation + h_matrix(i)
END DO
OMPVV_TEST_AND_SET_VERBOSE(errors, (num_dev * N) .ne. summation)
CALL omp_set_default_device(def_dev)
test_set_default_dev = errors
END FUNCTION test_set_default_dev
INTEGER FUNCTION test_device()
INTEGER :: num_dev, def_dev, summation, errors, i, dev
LOGICAL, DIMENSION(0:5000) :: isHost ! Arbitrary number greater than the num
INTEGER, DIMENSION(N) :: h_matrix
CHARACTER(len = 400) :: numDevMsg, defDevMsg, initDevMsg, resultsMsg
! Initialize vars
errors = 0
summation = 0
OMPVV_INFOMSG("test_device()")
! Get number of devices
num_dev = omp_get_num_devices()
WRITE(numDevMsg, *) "num_devices:", num_dev
OMPVV_INFOMSG(numDevMsg)
def_dev = omp_get_default_device()
WRITE(initDevMsg, *) "initial device is:", omp_get_initial_device()
OMPVV_INFOMSG(initDevMsg)
WRITE(defDevMsg, *) "default device is:", def_dev
OMPVV_INFOMSG(defDevMsg)
! Mapping the array to all of the devices
DO dev = 0, num_dev - 1
!$omp target enter data map (alloc: h_matrix) device(dev)
END DO
! Initialize the array
DO i = 1, N
h_matrix(i) = 0
END DO
! Each device gets updated with the current array version,
! one gets added to each element in the array, and then
! the host gets the updated version
DO dev = 0, num_dev - 1
!$omp target update to(h_matrix) device(dev)
!$omp target map(alloc: h_matrix) map(tofrom: isHost(dev)) device(dev)
isHost(dev) = omp_is_initial_device()
DO i = 1, N
h_matrix(i) = h_matrix(i) + 1
END DO
!$omp end target
!$omp target update from(h_matrix) device(dev)
END DO
! Unmap the matrix
DO dev = 0, num_dev - 1
!$omp target exit data map(delete: h_matrix) device(dev)
END DO
! Checking results
DO dev = 0, num_dev - 1
IF (isHost(dev) .eqv. .TRUE.) THEN
WRITE(resultsMsg, *) "device",dev,"ran on the host"
OMPVV_INFOMSG(resultsMsg)
ELSE
WRITE(resultsMsg, *) "device",dev,"ran on the device"
OMPVV_INFOMSG(resultsMsg)
END IF
END DO
! Checking results
DO i = 1, N
summation = summation + h_matrix(i)
END DO
OMPVV_TEST_AND_SET_VERBOSE(errors, (num_dev * N) .ne. summation)
test_device = errors
END FUNCTION test_device
END PROGRAM target_update_devices