Adding support for reading Environmental variable for samples (#186)

* check if env var set and choose device

* clean up for env var

* app modification

* move getEnvVar function to header

* fork example

* spacing adjust

* review comments

* use hipGetErrorName to throw errors

[ROCm/rocdecode commit: 8ec38d2d24]
This commit is contained in:
Lakshmi Kumar
2024-01-17 19:57:31 -08:00
committed by GitHub
parent e416b5a192
commit a186a42593
4 changed files with 41 additions and 19 deletions
@@ -144,14 +144,10 @@ int main(int argc, char **argv) {
ERR("ERROR: didn't find any GPU!");
return -1;
}
if (device_id >= num_devices) {
ERR("ERROR: the requested device_id is not found! ");
return -1;
}
hip_status = hipGetDeviceProperties(&hip_dev_prop, device_id);
if (hip_status != hipSuccess) {
ERR("ERROR: hipGetDeviceProperties for device (" +TOSTR(device_id) + " ) failed! (" + TOSTR(hip_status) + ")" );
ERR("ERROR: hipGetDeviceProperties for device (" +TOSTR(device_id) + " ) failed! (" + hipGetErrorName(hip_status) + ")" );
return -1;
}
@@ -164,13 +160,21 @@ int main(int argc, char **argv) {
sd = 1;
}
int hip_vis_dev_count = 0;
GetEnvVar("HIP_VISIBLE_DEVICES", hip_vis_dev_count);
for (int i = 0; i < n_fork; i++) {
std::unique_ptr<VideoDemuxer> demuxer(new VideoDemuxer(input_file_path.c_str()));
rocDecVideoCodec rocdec_codec_id = AVCodec2RocDecVideoCodec(demuxer->GetCodecID());
if (device_id % 2 == 0)
v_device_id[i] = (i % 2 == 0) ? device_id : device_id + sd;
else
v_device_id[i] = (i % 2 == 0) ? device_id - sd : device_id;
if (!hip_vis_dev_count) {
if (device_id % 2 == 0)
v_device_id[i] = (i % 2 == 0) ? device_id : device_id + sd;
else
v_device_id[i] = (i % 2 == 0) ? device_id - sd : device_id;
} else {
v_device_id[i] = i % hip_vis_dev_count;
}
std::unique_ptr<RocVideoDecoder> dec(new RocVideoDecoder(v_device_id[i], mem_type, rocdec_codec_id, b_force_zero_latency, p_crop_rect));
v_demuxer.push_back(std::move(demuxer));
v_viddec.push_back(std::move(dec));
@@ -147,14 +147,10 @@ int main(int argc, char **argv) {
ERR("ERROR: didn't find any GPU!");
return -1;
}
if (device_id >= num_devices) {
ERR("ERROR: the requested device_id is not found! ");
return -1;
}
hip_status = hipGetDeviceProperties(&hip_dev_prop, device_id);
if (hip_status != hipSuccess) {
ERR("ERROR: hipGetDeviceProperties for device (" +TOSTR(device_id) + " ) failed! (" + TOSTR(hip_status) + ")" );
ERR("ERROR: hipGetDeviceProperties for device (" +TOSTR(device_id) + " ) failed! (" + hipGetErrorName(hip_status) + ")" );
return -1;
}
@@ -171,13 +167,20 @@ int main(int argc, char **argv) {
std::vector<std::unique_ptr<RocVideoDecoder>> v_viddec;
std::vector<int> v_device_id(n_thread);
int hip_vis_dev_count = 0;
GetEnvVar("HIP_VISIBLE_DEVICES", hip_vis_dev_count);
for (int i = 0; i < n_thread; i++) {
std::unique_ptr<VideoDemuxer> demuxer(new VideoDemuxer(input_file_path.c_str()));
rocDecVideoCodec rocdec_codec_id = AVCodec2RocDecVideoCodec(demuxer->GetCodecID());
if (device_id % 2 == 0)
v_device_id[i] = (i % 2 == 0) ? device_id : device_id + sd;
else
v_device_id[i] = (i % 2 == 0) ? device_id - sd : device_id;
if (!hip_vis_dev_count) {
if (device_id % 2 == 0)
v_device_id[i] = (i % 2 == 0) ? device_id : device_id + sd;
else
v_device_id[i] = (i % 2 == 0) ? device_id - sd : device_id;
} else {
v_device_id[i] = i % hip_vis_dev_count;
}
std::unique_ptr<RocVideoDecoder> dec(new RocVideoDecoder(v_device_id[i], mem_type, rocdec_codec_id, b_force_zero_latency, p_crop_rect));
v_demuxer.push_back(std::move(demuxer));
v_viddec.push_back(std::move(dec));
@@ -1078,3 +1078,15 @@ bool RocVideoDecoder::InitHIP(int device_id) {
HIP_API_CALL(hipStreamCreate(&hip_stream_));
return true;
}
int GetEnvVar(const char *name, int &dev_count) {
char *v = std::getenv(name);
if (v) {
char* p_tkn = std::strtok(v, ",");
while (p_tkn != nullptr) {
dev_count++;
p_tkn = strtok(nullptr, ",");
}
}
return dev_count;
}
@@ -33,6 +33,7 @@ THE SOFTWARE.
#include <queue>
#include <stdexcept>
#include <exception>
#include <cstring>
#include <hip/hip_runtime.h>
extern "C" {
#include "libavutil/md5.h"
@@ -149,6 +150,8 @@ typedef struct ReconfigParams_t {
uint32_t reconfig_flush_mode;
} ReconfigParams;
int GetEnvVar(const char *name, int &dev_count);
class RocVideoDecoder {
public:
/**