Skip to content

Commit 73c32ec

Browse files
NguyenNhuDiumfranzwamd-garydengstanleytsang-amdeidenyoshida
authored
memory leak fix for rocRAND unit test (#571)
* Remove website URL from comments (#542) Referencing or using code from some websites is prohibited in this repository. This change removes an informational reference in the comments. * Add gfx1151 target (#543) (#546) Co-authored-by: Stanley Tsang <[email protected]> Co-authored-by: Eiden Yoshida <[email protected]> * Move data type support file along with index and ToC changes (#548) (#549) * added hipFree to test_rocrand_cpp_basic * fixed memory leak for test_rocrand_config_dispatch * fixed a memory leak in test_utils * changed createGraph to createAndLaunchGraph, as well as fixed stream capture order * changed default boolean (kaunchGraph, sync) to be true in createAndLaunchGraph * added back missing end stream capture * reformated curlys for consistency * removed createAndLaunchGraph inside resetGraphHelper --------- Co-authored-by: Wayne Franz <[email protected]> Co-authored-by: amd-garydeng <[email protected]> Co-authored-by: Stanley Tsang <[email protected]> Co-authored-by: Eiden Yoshida <[email protected]> Co-authored-by: Jeffrey Novotny <[email protected]>
1 parent 81d9e58 commit 73c32ec

File tree

5 files changed

+86
-85
lines changed

5 files changed

+86
-85
lines changed

Diff for: scripts/copyright-date/check-copyright.sh

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ if $forkdiff; then
6161
source_commit="remotes/$remote/HEAD"
6262

6363
# don't use fork-point for finding fork point (lol)
64-
# see: https://stackoverflow.com/a/53981615
6564
diff_hash="$(git merge-base "$source_commit" "$branch")"
6665
fi
6766

Diff for: test/internal/test_rocrand_config_dispatch.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ TEST(rocrand_config_dispatch_tests, host_matches_device)
8989

9090
ASSERT_NE(host_arch, rocrand_impl::host::target_arch::invalid);
9191
ASSERT_EQ(host_arch, device_arch);
92+
93+
HIP_CHECK(hipFree(device_arch_ptr));
9294
}
9395

9496
TEST(rocrand_config_dispatch_tests, parse_common_architectures)

Diff for: test/test_rocrand_cpp_basic.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ TYPED_TEST(rocrand_cpp_basic_tests, move_construction)
8484

8585
float actual;
8686
HIP_CHECK(hipMemcpy(&actual, d_data, sizeof(actual), hipMemcpyDeviceToHost));
87-
8887
ASSERT_EQ(expected, actual);
88+
89+
HIP_CHECK(hipFree(d_data));
8990
}
9091

9192
TYPED_TEST(rocrand_cpp_basic_tests, move_assignment)
@@ -119,6 +120,7 @@ TYPED_TEST(rocrand_cpp_basic_tests, move_assignment)
119120

120121
float actual;
121122
HIP_CHECK(hipMemcpy(&actual, d_data, sizeof(actual), hipMemcpyDeviceToHost));
122-
123123
ASSERT_EQ(expected, actual);
124+
125+
HIP_CHECK(hipFree(d_data));
124126
}

Diff for: test/test_rocrand_hipgraphs.cpp

+25-25
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,34 @@ void test_float(std::function<rocrand_status(rocrand_generator, float*, size_t,
3434
HIP_CHECK(hipStreamCreateWithFlags(&stream, hipStreamNonBlocking));
3535
rocrand_set_stream(generator, stream);
3636

37-
hipGraphExec_t graph_instance;
38-
hipGraph_t graph = test_utils::createGraphHelper(stream);
37+
test_utils::GraphHelper gHelper;
38+
39+
gHelper.startStreamCapture(stream);
3940

4041
// Any sizes
4142
ROCRAND_CHECK(
4243
generate_fn(generator, data, 1, mean, stddev)
4344
);
44-
45-
graph_instance = test_utils::endCaptureGraphHelper(graph, stream, true, true);
46-
test_utils::resetGraphHelper(graph, graph_instance, stream);
45+
gHelper.createAndLaunchGraph(stream);
46+
gHelper.resetGraphHelper(stream);
4747

4848
// Any alignment
4949
ROCRAND_CHECK(
5050
generate_fn(generator, data+1, 2, mean, stddev)
5151
);
5252

53-
graph_instance = test_utils::endCaptureGraphHelper(graph, stream, true, true);
54-
test_utils::resetGraphHelper(graph, graph_instance, stream);
53+
gHelper.createAndLaunchGraph(stream);
54+
gHelper.resetGraphHelper(stream);
5555

5656
ROCRAND_CHECK(
5757
generate_fn(generator, data, size, mean, stddev)
5858
);
5959

60-
graph_instance = test_utils::endCaptureGraphHelper(graph, stream, true, true);
60+
gHelper.createAndLaunchGraph(stream);
6161

6262
HIP_CHECK(hipFree(data));
6363
ROCRAND_CHECK(rocrand_destroy_generator(generator));
64-
test_utils::cleanupGraphHelper(graph, graph_instance);
64+
gHelper.cleanupGraphHelper();
6565
HIP_CHECK(hipStreamDestroy(stream));
6666
}
6767

@@ -109,34 +109,34 @@ TEST_P(rocrand_hipgraph_generate_tests, uniform_float_test)
109109
HIP_CHECK(hipStreamCreateWithFlags(&stream, hipStreamNonBlocking));
110110
rocrand_set_stream(generator, stream);
111111

112-
hipGraphExec_t graph_instance;
113-
hipGraph_t graph = test_utils::createGraphHelper(stream);
112+
test_utils::GraphHelper gHelper;
113+
gHelper.startStreamCapture(stream);
114114

115115
// Any sizes
116116
ROCRAND_CHECK(
117117
rocrand_generate_uniform(generator, data, 1)
118118
);
119119

120-
graph_instance = test_utils::endCaptureGraphHelper(graph, stream, true, true);
121-
test_utils::resetGraphHelper(graph, graph_instance, stream);
120+
gHelper.createAndLaunchGraph(stream);
121+
gHelper.resetGraphHelper(stream);
122122

123123
// Any alignment
124124
ROCRAND_CHECK(
125125
rocrand_generate_uniform(generator, data+1, 2)
126126
);
127127

128-
graph_instance = test_utils::endCaptureGraphHelper(graph, stream, true, true);
129-
test_utils::resetGraphHelper(graph, graph_instance, stream);
128+
gHelper.createAndLaunchGraph(stream);
129+
gHelper.resetGraphHelper(stream);
130130

131131
ROCRAND_CHECK(
132132
rocrand_generate_uniform(generator, data, size)
133133
);
134134

135-
graph_instance = test_utils::endCaptureGraphHelper(graph, stream, true, true);
135+
gHelper.createAndLaunchGraph(stream);
136136

137137
HIP_CHECK(hipFree(data));
138138
ROCRAND_CHECK(rocrand_destroy_generator(generator));
139-
test_utils::cleanupGraphHelper(graph, graph_instance);
139+
gHelper.cleanupGraphHelper();
140140
HIP_CHECK(hipStreamDestroy(stream));
141141
}
142142

@@ -159,28 +159,28 @@ TEST_P(rocrand_hipgraph_generate_tests, poisson_test)
159159
HIP_CHECK(hipStreamCreateWithFlags(&stream, hipStreamNonBlocking));
160160
rocrand_set_stream(generator, stream);
161161

162-
hipGraphExec_t graph_instance;
163-
hipGraph_t graph = test_utils::createGraphHelper(stream);
162+
test_utils::GraphHelper gHelper;
163+
gHelper.startStreamCapture(stream);
164164

165165
// Any sizes
166166
ROCRAND_CHECK(rocrand_generate_poisson(generator, data, 1, 10.0));
167167

168-
graph_instance = test_utils::endCaptureGraphHelper(graph, stream, true, true);
169-
test_utils::resetGraphHelper(graph, graph_instance, stream);
168+
gHelper.createAndLaunchGraph(stream);
169+
gHelper.resetGraphHelper(stream);
170170

171171
// Any alignment
172172
ROCRAND_CHECK(rocrand_generate_poisson(generator, data + 1, 2, 500.0));
173173

174-
graph_instance = test_utils::endCaptureGraphHelper(graph, stream, true, true);
175-
test_utils::resetGraphHelper(graph, graph_instance, stream);
174+
gHelper.createAndLaunchGraph(stream);
175+
gHelper.resetGraphHelper(stream);
176176

177177
ROCRAND_CHECK(rocrand_generate_poisson(generator, data, size, 5000.0));
178178

179-
graph_instance = test_utils::endCaptureGraphHelper(graph, stream, true, true);
179+
gHelper.createAndLaunchGraph(stream);
180180

181181
HIP_CHECK(hipFree(data));
182182
ROCRAND_CHECK(rocrand_destroy_generator(generator));
183-
test_utils::cleanupGraphHelper(graph, graph_instance);
183+
gHelper.cleanupGraphHelper();
184184
HIP_CHECK(hipStreamDestroy(stream));
185185
}
186186

Diff for: test/test_utils_hipgraphs.hpp

+55-57
Original file line numberDiff line numberDiff line change
@@ -28,64 +28,62 @@
2828
// Note: graphs will not work on the default stream.
2929
namespace test_utils
3030
{
31+
class GraphHelper{
32+
private:
33+
hipGraph_t graph;
34+
hipGraphExec_t graph_instance;
35+
public:
36+
37+
inline void startStreamCapture(hipStream_t & stream)
38+
{
39+
HIP_CHECK_NON_VOID(hipStreamBeginCapture(stream, hipStreamCaptureModeGlobal));
40+
}
41+
42+
inline void endStreamCapture(hipStream_t & stream)
43+
{
44+
HIP_CHECK_NON_VOID(hipStreamEndCapture(stream, &graph));
45+
}
46+
47+
inline void createAndLaunchGraph(hipStream_t & stream, const bool launchGraph=true, const bool sync=true)
48+
{
49+
50+
endStreamCapture(stream);
51+
52+
HIP_CHECK_NON_VOID(hipGraphInstantiate(&graph_instance, graph, nullptr, nullptr, 0));
53+
54+
// Optionally launch the graph
55+
if (launchGraph)
56+
HIP_CHECK_NON_VOID(hipGraphLaunch(graph_instance, stream));
57+
58+
// Optionally synchronize the stream when we're done
59+
if (sync)
60+
HIP_CHECK_NON_VOID(hipStreamSynchronize(stream));
61+
}
3162

32-
inline hipGraph_t createGraphHelper(hipStream_t& stream, const bool beginCapture=true)
33-
{
34-
// Create a new graph
35-
hipGraph_t graph;
36-
HIP_CHECK_NON_VOID(hipGraphCreate(&graph, 0));
37-
38-
// Optionally begin stream capture
39-
if (beginCapture)
40-
HIP_CHECK_NON_VOID(hipStreamBeginCapture(stream, hipStreamCaptureModeGlobal));
41-
42-
return graph;
43-
}
44-
45-
inline void cleanupGraphHelper(hipGraph_t& graph, hipGraphExec_t& instance)
46-
{
47-
HIP_CHECK_NON_VOID(hipGraphDestroy(graph));
48-
HIP_CHECK_NON_VOID(hipGraphExecDestroy(instance));
49-
}
50-
51-
inline void resetGraphHelper(hipGraph_t& graph, hipGraphExec_t& instance, hipStream_t& stream, const bool beginCapture=true)
52-
{
53-
// Destroy the old graph and instance
54-
cleanupGraphHelper(graph, instance);
55-
56-
// Create a new graph and optionally begin capture
57-
graph = createGraphHelper(stream, beginCapture);
58-
}
59-
60-
inline hipGraphExec_t endCaptureGraphHelper(hipGraph_t& graph, hipStream_t& stream, const bool launchGraph=false, const bool sync=false)
61-
{
62-
// End the capture
63-
HIP_CHECK_NON_VOID(hipStreamEndCapture(stream, &graph));
64-
65-
// Instantiate the graph
66-
hipGraphExec_t instance;
67-
HIP_CHECK_NON_VOID(hipGraphInstantiate(&instance, graph, nullptr, nullptr, 0));
68-
69-
// Optionally launch the graph
70-
if (launchGraph)
71-
HIP_CHECK_NON_VOID(hipGraphLaunch(instance, stream));
72-
73-
// Optionally synchronize the stream when we're done
74-
if (sync)
75-
HIP_CHECK_NON_VOID(hipStreamSynchronize(stream));
76-
77-
return instance;
78-
}
79-
80-
inline void launchGraphHelper(hipGraphExec_t& instance, hipStream_t& stream, const bool sync=false)
81-
{
82-
HIP_CHECK_NON_VOID(hipGraphLaunch(instance, stream));
83-
84-
// Optionally sync after the launch
85-
if (sync)
86-
HIP_CHECK_NON_VOID(hipStreamSynchronize(stream));
87-
}
88-
63+
inline void cleanupGraphHelper()
64+
{
65+
HIP_CHECK_NON_VOID(hipGraphDestroy(this->graph));
66+
HIP_CHECK_NON_VOID(hipGraphExecDestroy(this->graph_instance));
67+
}
68+
69+
inline void resetGraphHelper(hipStream_t& stream, const bool beginCapture=true)
70+
{
71+
// Destroy the old graph and instance
72+
cleanupGraphHelper();
73+
74+
if(beginCapture)
75+
startStreamCapture(stream);
76+
}
77+
78+
inline void launchGraphHelper(hipStream_t& stream,const bool sync=false)
79+
{
80+
HIP_CHECK_NON_VOID(hipGraphLaunch(this->graph_instance, stream));
81+
82+
// Optionally sync after the launch
83+
if (sync)
84+
HIP_CHECK_NON_VOID(hipStreamSynchronize(stream));
85+
}
86+
};
8987
} // end namespace test_utils
9088

9189
#endif //ROCRAND_TEST_UTILS_HIPGRAPHS_HPP

0 commit comments

Comments
 (0)