Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,44 @@
* SPDX-License-Identifier: MIT
*/

#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <random>
#include <string>

#include <hip/hip_runtime_api.h>
#include <hip_test_common.hh>
#include <hip_test_filesystem.hh>
#include <contract_cleanup.hh>

namespace {
constexpr size_t kByteCount = 64;

// Builds a unique temporary file path for the exported dot graph so concurrent
// test binaries do not collide on a shared filename.
std::string UniqueDotPath() {
// Random suffix for temp artifacts, the same way hip_test_process.hh names its
// capture files. Random rather than derived from the pid: a guessable path in a
// shared temp directory can be pre-seeded with a symlink, which the export then
// follows. The clock is mixed in so the name stays unique even where
// std::random_device is a weak source.
std::string RandomTag() {
std::random_device dev;
uint64_t value = (static_cast<uint64_t>(dev()) << 32) ^ dev();
value ^= static_cast<uint64_t>(
std::chrono::high_resolution_clock::now().time_since_epoch().count());
char buf[17];
snprintf(buf, sizeof(buf), "%016llx", static_cast<unsigned long long>(value));
return std::string(buf);
}

// Use the temp directory because installed test directories may be read-only.
std::string DotPath() {
int device = 0;
HIP_CHECK(hipGetDevice(&device));
return std::string("hip_contract_graph_debug_") + std::to_string(device) + "_" +
std::to_string(static_cast<long long>(reinterpret_cast<intptr_t>(&device))) + ".dot";
return (fs::temp_directory_path() /
("hip_contract_graph_debug_" + std::to_string(device) + "_" + RandomTag() + ".dot"))
.string();
Comment thread
iassiour marked this conversation as resolved.
Resolved
}

hipMemsetParams MakeByteMemsetParams(void* device_ptr, unsigned int value) {
Expand Down Expand Up @@ -61,8 +79,7 @@ HIP_TEST_CASE(Contract_GraphDebug_HipGraphDebugDotPrint_Default_WritesNonEmptyFi
hipMemsetParams memset_params = MakeByteMemsetParams(device_ptr, 0x5A);
HIP_CHECK(hipGraphAddMemsetNode(&node, graph, nullptr, 0, &memset_params));

const std::string path = UniqueDotPath();
std::remove(path.c_str());
const std::string path = DotPath();

// Exporting a non-empty graph to a dot file must succeed (or report the
// feature unsupported) and, on success, produce a non-empty file on disk.
Expand All @@ -87,8 +104,7 @@ HIP_TEST_CASE(Contract_GraphDebug_HipGraphDebugDotPrint_Default_VerboseFlagIsAcc
cleanup.Add([graph] { (void)hipGraphDestroy(graph); });
HIP_CHECK(hipGraphAddEmptyNode(&node, graph, nullptr, 0));

const std::string path = UniqueDotPath();
std::remove(path.c_str());
const std::string path = DotPath();

// The verbose flag augments the output but must not change the success
// contract: a valid graph still exports to a non-empty file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,35 @@
#include <hip/hip_runtime_api.h>
#include <hip/hiprtc.h>
#include <hip_test_common.hh>
#include <hip_test_filesystem.hh>

#include <chrono>
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <random>
#include <string>
#include <vector>

// The library-from-file and managed-symbol APIs (hipLibraryLoadFromFile,
// hipLibraryGetManaged) are exercised on both backends: on NVIDIA they map to
// the CUDA driver cuLibrary* entry points.
namespace {
// Random suffix for temp artifacts, the same way hip_test_process.hh names its
// capture files. Random rather than derived from the pid: a guessable path in a
// shared temp directory can be pre-seeded with a symlink, which the subsequent
// open then follows. The clock is mixed in so the name stays unique even where
// std::random_device is a weak source.
std::string RandomTag() {
std::random_device dev;
uint64_t value = (static_cast<uint64_t>(dev()) << 32) ^ dev();
value ^= static_cast<uint64_t>(
std::chrono::high_resolution_clock::now().time_since_epoch().count());
char buf[17];
snprintf(buf, sizeof(buf), "%016llx", static_cast<unsigned long long>(value));
return std::string(buf);
}

constexpr char const kWriteKernelName[] = "write_value";

constexpr char const kLibrarySource[] =
Expand Down Expand Up @@ -68,15 +87,17 @@ bool CompileLibrarySource(std::vector<char>& code) {
return true;
}

// Compiles the library source and writes it to a unique per-test file in the
// current working directory, returning the path. Skips when HIPRTC is
// unavailable. The caller must remove the file when done.
// Compile to a temp file: installed test directories may be read-only.
// Returns the path; the caller must remove the file.
std::string WriteCodeObjectFile(const char* suffix) {
std::vector<char> code;
if (!CompileLibrarySource(code)) {
HIP_SKIP_TEST("HIPRTC compilation is not supported by this device/runtime path.");
}
const std::string path = std::string("hip-contract-library-file-") + suffix + ".code";
const std::string path = (fs::temp_directory_path() /
("hip-contract-library-file-" + std::string(suffix) + "-" +
RandomTag() + ".code"))
.string();
Comment thread
iassiour marked this conversation as resolved.
Resolved
std::ofstream out(path, std::ios::binary);
REQUIRE(out.is_open());
out.write(code.data(), static_cast<std::streamsize>(code.size()));
Expand Down
Loading