Commit 473916f2 by songxinkai

bazel/new_local_repository, cuda/nvml, c++/openssl/md5

parent 7f4d795c
package(default_visibility = ["//visibility:public"])
cc_library(
name="nvml",
hdrs=["nvml.h"],
)
new_local_repository(
name="cuda",
path="/usr/local/cuda/include",
build_file="BUILD.nvml",
# build_file_content = """
#package(default_visibility = ["//visibility:public"])
#cc_library(
# name="nvml",
# hdrs=["nvml.h",],
#)
#"""
)
cc_binary(
name = "main",
deps = ["@cuda//:nvml",],
srcs = ["main.cpp",],
linkopts=[
"-L/usr/local/cuda/lib64/stubs",
"-lnvidia-ml",
],
copts=[
"-Iexternal/cuda",
],
)
#include <nvml.h>
#include <iostream>
using std::cout;
using std::endl;
int main(){
unsigned int device_id = 0, device_count;
cout << "Using GPU " << device_id << "." << endl;
nvmlDevice_t device;
nvmlReturn_t result;
result = nvmlInit();
result = nvmlDeviceGetCount(&device_count);
result = nvmlDeviceGetHandleByIndex(device_id, &device);
if (NVML_SUCCESS != result) {
cout << "Failed to get handle for device " << device_id << ": " << nvmlErrorString(result) << endl;
return -1;
}
nvmlUtilization_t utilization;
result = nvmlDeviceGetUtilizationRates(device, &utilization);
cout << "GPU " << device_id << ", utilization: " << utilization.gpu << endl;
cout << "GPU " << device_id << ", memory: " << utilization.memory << endl;
return 0;
}
...@@ -10,4 +10,5 @@ int main(){ ...@@ -10,4 +10,5 @@ int main(){
cin >> a; cin >> a;
cout << a << endl; cout << a << endl;
cout << int(std::ceil(a)) << endl; cout << int(std::ceil(a)) << endl;
cout << std::fabs(a) << endl;
} }
// g++ --std=c++11 -lssl -lcrypto get_md5sum.cpp
#include <fstream>
#include <iostream>
#include <openssl/md5.h>
#include <string>
#include <cstring>
using std::string;
using std::cout;
using std::endl;
int get_file_md5(const std::string &file_name, std::string &md5_value) {
md5_value.clear();
std::ifstream file(file_name.c_str(), std::ifstream::binary);
if (!file) { return -1; }
MD5_CTX md5Context;
MD5_Init(&md5Context);
char buf[1024 * 16];
while (file.good()) {
file.read(buf, sizeof(buf));
MD5_Update(&md5Context, buf, file.gcount());
}
unsigned char result[MD5_DIGEST_LENGTH];
MD5_Final(result, &md5Context);
char hex[35];
memset(hex, 0, sizeof(hex));
for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
sprintf(hex + i * 2, "%02x", result[i]);
}
hex[32] = '\0';
md5_value = string(hex);
return 0;
}
int main(int argc, char* argv[]) {
string file_name = "/home/songxinkai/alphazero-selfplay-tensrort/share/caffemodels/best_models/agz_19b_best.caffemodel";
string md5value;
int ret = get_file_md5(file_name, md5value);
if (ret < 0) {
printf("get file md5 failed. file: %s\n", file_name.c_str());
return -1;
}
printf("the md5value: %s\n", md5value.c_str());
}
...@@ -66,5 +66,15 @@ int main(){ ...@@ -66,5 +66,15 @@ int main(){
int i; int i;
s2i(vec_s_1[0], i); s2i(vec_s_1[0], i);
cout << i << endl; cout << i << endl;
string a = "asdf";
string b = "asdf";
cout << a << ", " << b << endl;
cout << &a << ", " << &b << endl;
a = b;
cout << a << ", " << b << endl;
cout << &a << ", " << &b << endl;
b = "123";
cout << a << ", " << b << endl;
cout << &a << ", " << &b << endl;
return 0; return 0;
} }
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
int main(){
stringstream ss;
string s = "asdf";
cout << s.find("df")
<< ", " << (s.find("f") == string::npos)
<< ", " << (s.find(",") == string::npos)
<< endl;
s="2.3";
float a = 0;
ss << s;
ss >> a;
cout << ss.str() << ", " << a << endl;
cout << to_string(a) << endl;
return 0;
}
...@@ -43,7 +43,7 @@ int main(){ ...@@ -43,7 +43,7 @@ int main(){
} }
// GPU device start // GPU device start
int device_id = 1; int device_id = 2;
CUDA_CHECK(cudaSetDevice(device_id)); CUDA_CHECK(cudaSetDevice(device_id));
cout << "Using GPU " << device_id << "." << endl; cout << "Using GPU " << device_id << "." << endl;
......
#include <nvml.h>
#include <iostream>
using std::cout;
using std::endl;
int main(){
unsigned int device_id = 0, device_count;
cout << "Using GPU " << device_id << "." << endl;
nvmlDevice_t device;
nvmlReturn_t result;
result = nvmlInit();
result = nvmlDeviceGetCount(&device_count);
result = nvmlDeviceGetHandleByIndex(device_id, &device);
if (NVML_SUCCESS != result) {
cout << "Failed to get handle for device " << device_id << ": " << nvmlErrorString(result) << endl;
return -1;
}
nvmlUtilization_t utilization;
result = nvmlDeviceGetUtilizationRates(device, &utilization);
cout << "GPU " << device_id << ", utilization: " << utilization.gpu << endl;
cout << "GPU " << device_id << ", memory: " << utilization.memory << endl;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment