Skip to content

Commit 7208790

Browse files
authored
Grcuda 132 refactor deviceselectionpolicy in grcudastreampolicy (#50)
* Each policy type has a separated class * Keep only retrieveImpl * Now TransferTimeDeviceSelectionPolicy extends DeviceSelectionPolicy * Delete previously commented methods and clean code * added license for each file
1 parent 85dcf30 commit 7208790

File tree

10 files changed

+823
-472
lines changed

10 files changed

+823
-472
lines changed

projects/com.nvidia.grcuda.test/src/com/nvidia/grcuda/test/runtime/executioncontext/GrCUDAStreamPolicyMockTest.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
/*
2+
* Copyright (c) 2020, 2021, NECSTLab, Politecnico di Milano. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of NECSTLab nor the names of its
13+
* contributors may be used to endorse or promote products derived
14+
* from this software without specific prior written permission.
15+
* * Neither the name of Politecnico di Milano nor the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
20+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
131
package com.nvidia.grcuda.test.runtime.executioncontext;
232

333
import com.nvidia.grcuda.GrCUDAOptionMap;
@@ -7,6 +37,8 @@
737
import com.nvidia.grcuda.runtime.computation.dependency.DependencyPolicyEnum;
838
import com.nvidia.grcuda.runtime.stream.policy.DeviceSelectionPolicyEnum;
939
import com.nvidia.grcuda.runtime.stream.policy.GrCUDAStreamPolicy;
40+
import com.nvidia.grcuda.runtime.stream.policy.RoundRobinDeviceSelectionPolicy;
41+
import com.nvidia.grcuda.runtime.stream.policy.TransferTimeDeviceSelectionPolicy;
1042
import com.nvidia.grcuda.runtime.stream.policy.RetrieveNewStreamPolicyEnum;
1143
import com.nvidia.grcuda.runtime.stream.policy.RetrieveParentStreamPolicyEnum;
1244
import com.nvidia.grcuda.test.util.mock.AsyncGrCUDAExecutionContextMock;
@@ -41,14 +73,14 @@ private static AsyncGrCUDAExecutionContextMock createContext(int numberOfGPUs, D
4173
);
4274
}
4375

44-
private GrCUDAStreamPolicy.RoundRobinDeviceSelectionPolicy getRoundRobinPolicy(int numGPUs) {
76+
private RoundRobinDeviceSelectionPolicy getRoundRobinPolicy(int numGPUs) {
4577
GrCUDADevicesManagerMock devicesManager = new GrCUDADevicesManagerMock(new DeviceListMock(numGPUs), numGPUs);
46-
return new GrCUDAStreamPolicy.RoundRobinDeviceSelectionPolicy(devicesManager);
78+
return new RoundRobinDeviceSelectionPolicy(devicesManager);
4779
}
4880

4981
@Test
5082
public void roundRobinTest() {
51-
GrCUDAStreamPolicy.RoundRobinDeviceSelectionPolicy policy = getRoundRobinPolicy(4);
83+
RoundRobinDeviceSelectionPolicy policy = getRoundRobinPolicy(4);
5284
Device d = policy.retrieve(null);
5385
assertEquals(0, d.getDeviceId());
5486
assertEquals(1, policy.getInternalState());
@@ -126,7 +158,7 @@ public void createBandwidthMatrixTest() {
126158
{45, 60, 20},
127159
{10, 20, 0}
128160
};
129-
double[][] b = ((GrCUDAStreamPolicy.TransferTimeDeviceSelectionPolicy) streamPolicy.getDeviceSelectionPolicy()).getLinkBandwidth();
161+
double[][] b = ((TransferTimeDeviceSelectionPolicy) streamPolicy.getDeviceSelectionPolicy()).getLinkBandwidth();
130162
for (int i = 0; i < b.length; i++) {
131163
for (int j = 0; j < b[i].length; j++) {
132164
assertEquals(bGold[i][j], b[i][j], 1e-6);
@@ -137,7 +169,7 @@ public void createBandwidthMatrixTest() {
137169
@Test
138170
public void bandwidthComputationMinMaxTest() {
139171
AsyncGrCUDAExecutionContextMock context = createContext(2, DeviceSelectionPolicyEnum.MINMAX_TRANSFER_TIME);
140-
GrCUDAStreamPolicy.TransferTimeDeviceSelectionPolicy deviceSelectionPolicy = (GrCUDAStreamPolicy.TransferTimeDeviceSelectionPolicy) ((GrCUDAStreamPolicyMock) context.getStreamManager().getStreamPolicy()).getDeviceSelectionPolicy();
172+
TransferTimeDeviceSelectionPolicy deviceSelectionPolicy = (TransferTimeDeviceSelectionPolicy) ((GrCUDAStreamPolicyMock) context.getStreamManager().getStreamPolicy()).getDeviceSelectionPolicy();
141173
// If data is updated on the target device, we have infinite bandwidth (regardless of what's on the matrix diagonal);
142174
double b = deviceSelectionPolicy.computeBandwidth(0, new HashSet<>(Arrays.asList(0, 1, CPUDevice.CPU_DEVICE_ID)));
143175
assertEquals(Double.POSITIVE_INFINITY, b, 1e-6);
@@ -149,7 +181,7 @@ public void bandwidthComputationMinMaxTest() {
149181
@Test
150182
public void bandwidthComputationMinMinTest() {
151183
AsyncGrCUDAExecutionContextMock context = createContext(2, DeviceSelectionPolicyEnum.MINMIN_TRANSFER_TIME);
152-
GrCUDAStreamPolicy.TransferTimeDeviceSelectionPolicy deviceSelectionPolicy = (GrCUDAStreamPolicy.TransferTimeDeviceSelectionPolicy) ((GrCUDAStreamPolicyMock) context.getStreamManager().getStreamPolicy()).getDeviceSelectionPolicy();
184+
TransferTimeDeviceSelectionPolicy deviceSelectionPolicy = (TransferTimeDeviceSelectionPolicy) ((GrCUDAStreamPolicyMock) context.getStreamManager().getStreamPolicy()).getDeviceSelectionPolicy();
153185
// If data is updated on the target device, we have infinite bandwidth (regardless of what's on the matrix diagonal);
154186
double b = deviceSelectionPolicy.computeBandwidth(0, new HashSet<>(Arrays.asList(0, 1, CPUDevice.CPU_DEVICE_ID)));
155187
assertEquals(Double.POSITIVE_INFINITY, b, 1e-6);
@@ -161,15 +193,15 @@ public void bandwidthComputationMinMinTest() {
161193
@Test(expected = IllegalStateException.class)
162194
public void bandwidthComputationWithNoUpdatedLocationTest() {
163195
AsyncGrCUDAExecutionContextMock context = createContext(2, DeviceSelectionPolicyEnum.MINMAX_TRANSFER_TIME);
164-
GrCUDAStreamPolicy.TransferTimeDeviceSelectionPolicy deviceSelectionPolicy = (GrCUDAStreamPolicy.TransferTimeDeviceSelectionPolicy) ((GrCUDAStreamPolicyMock) context.getStreamManager().getStreamPolicy()).getDeviceSelectionPolicy();
196+
TransferTimeDeviceSelectionPolicy deviceSelectionPolicy = (TransferTimeDeviceSelectionPolicy) ((GrCUDAStreamPolicyMock) context.getStreamManager().getStreamPolicy()).getDeviceSelectionPolicy();
165197
// If the data is not available on any device, give an error;
166198
double b = deviceSelectionPolicy.computeBandwidth(0, new HashSet<>());
167199
}
168200

169201
@Test(expected = ArrayIndexOutOfBoundsException.class)
170202
public void bandwidthComputationOutOfBoundsLocationTest() {
171203
AsyncGrCUDAExecutionContextMock context = createContext(2, DeviceSelectionPolicyEnum.MINMAX_TRANSFER_TIME);
172-
GrCUDAStreamPolicy.TransferTimeDeviceSelectionPolicy deviceSelectionPolicy = (GrCUDAStreamPolicy.TransferTimeDeviceSelectionPolicy) ((GrCUDAStreamPolicyMock) context.getStreamManager().getStreamPolicy()).getDeviceSelectionPolicy();
204+
TransferTimeDeviceSelectionPolicy deviceSelectionPolicy = (TransferTimeDeviceSelectionPolicy) ((GrCUDAStreamPolicyMock) context.getStreamManager().getStreamPolicy()).getDeviceSelectionPolicy();
173205
// If the data is not available on any device, give an error;
174206
double b = deviceSelectionPolicy.computeBandwidth(10, new HashSet<>(Collections.singletonList(1)));
175207
}

projects/com.nvidia.grcuda/src/com/nvidia/grcuda/runtime/stream/policy/DeviceSelectionPolicy.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
1+
/*
2+
* Copyright (c) 2020, 2021, NECSTLab, Politecnico di Milano. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of NECSTLab nor the names of its
13+
* contributors may be used to endorse or promote products derived
14+
* from this software without specific prior written permission.
15+
* * Neither the name of Politecnico di Milano nor the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
20+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
131
package com.nvidia.grcuda.runtime.stream.policy;
232

333
import com.nvidia.grcuda.GrCUDAException;
434
import com.nvidia.grcuda.runtime.Device;
535
import com.nvidia.grcuda.runtime.array.AbstractArray;
636
import com.nvidia.grcuda.runtime.executioncontext.ExecutionDAG;
737

38+
import java.util.Comparator;
839
import java.util.List;
40+
import java.util.stream.Collectors;
941

1042
/**
1143
* When using multiple GPUs, selecting the stream where a computation is executed implies
@@ -47,7 +79,9 @@ public Device retrieve(ExecutionDAG.DAGVertex vertex, List<Device> devices) {
4779
} else if (devices.size() == 0) {
4880
throw new GrCUDAException("the list of devices where the computation can be executed is empty");
4981
} else {
50-
return this.retrieveImpl(vertex, devices);
82+
// Sort the devices by ID;
83+
List<Device> sortedDevices = devices.stream().sorted(Comparator.comparingInt(Device::getDeviceId)).collect(Collectors.toList());
84+
return this.retrieveImpl(vertex, sortedDevices);
5185
}
5286
}
5387

projects/com.nvidia.grcuda/src/com/nvidia/grcuda/runtime/stream/policy/DeviceSelectionPolicyEnum.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
/*
2+
* Copyright (c) 2020, 2021, NECSTLab, Politecnico di Milano. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of NECSTLab nor the names of its
13+
* contributors may be used to endorse or promote products derived
14+
* from this software without specific prior written permission.
15+
* * Neither the name of Politecnico di Milano nor the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
20+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
131
package com.nvidia.grcuda.runtime.stream.policy;
232

333
public enum DeviceSelectionPolicyEnum {

projects/com.nvidia.grcuda/src/com/nvidia/grcuda/runtime/stream/policy/GrCUDADevicesManager.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
/*
2+
* Copyright (c) 2020, 2021, NECSTLab, Politecnico di Milano. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of NECSTLab nor the names of its
13+
* contributors may be used to endorse or promote products derived
14+
* from this software without specific prior written permission.
15+
* * Neither the name of Politecnico di Milano nor the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
20+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
131
package com.nvidia.grcuda.runtime.stream.policy;
232

333
import java.util.Collection;

0 commit comments

Comments
 (0)