我正在开发一个项目,我需要我的CUDA设备在包含指针的结构上进行计算.
typedef struct StructA {
int* arr;
} StructA;
Run Code Online (Sandbox Code Playgroud)
当我为结构分配内存然后将其复制到设备时,它只会复制结构而不是指针的内容.现在我通过首先分配指针来解决这个问题,然后将主机结构设置为使用新指针(位于GPU上).以下代码示例使用上面的结构描述了此方法:
#define N 10
int main() {
int h_arr[N] = {1,2,3,4,5,6,7,8,9,10};
StructA *h_a = (StructA*)malloc(sizeof(StructA));
StructA *d_a;
int *d_arr;
// 1. Allocate device struct.
cudaMalloc((void**) &d_a, sizeof(StructA));
// 2. Allocate device pointer.
cudaMalloc((void**) &(d_arr), sizeof(int)*N);
// 3. Copy pointer content from host to device.
cudaMemcpy(d_arr, h_arr, sizeof(int)*N, cudaMemcpyHostToDevice);
// 4. Point to device pointer in host struct.
h_a->arr = d_arr;
// 5. Copy struct from host to device.
cudaMemcpy(d_a, h_a, sizeof(StructA), …Run Code Online (Sandbox Code Playgroud) 这是我在终端中看到的内容:
user@user-OptiPlex-9020:~$ sudo apt-get update
Ign http://extras.ubuntu.com trusty InRelease
Ign http://in.archive.ubuntu.com trusty InRelease
Get:1 http://dl.google.com stable InRelease
100% [1 InRelease gpgv 1,661 B] [Connecting to 192.168.0.90 (192.168.0.90)] [WaSplitting up /var/lib/apt/lists/partial/dl.google.com_linux_chrome_deb_dists_stable_InRelease intoIgn http://dl.google.com stable InRelease
E: GPG error: http://dl.google.com stable InRelease: Clearsigned file isn't valid, got 'NODATA' (does the network require authentication?)
Run Code Online (Sandbox Code Playgroud)
我在/etc/apt/apt.conf文件中有所需的身份验证信息:
Acquire::http::proxy "http://username:password@192.168.0.90:8080/";
Acquire::https::proxy "https://username:password@192.168.0.90:8080/";
Acquire::ftp::proxy "ftp://username:password@192.168.20.0:8080/";
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚这个问题.
在C++中我想初始化一个双矩阵(二维双数组),就像我通常没有像这样的指针:
double data[4][4] = {
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1
};
Run Code Online (Sandbox Code Playgroud)
但是,由于我想返回并将其传递给函数,我需要它作为double**指针.所以,基本上我需要以一种很好的方式初始化数据(如上所述),但之后我需要将指针保存到2D数组,而不会在函数退出时丢失数据.
对此有何帮助?:-)
我是tensorflow新手,我已经开始使用tensorflow 2.0
我为多类分类问题构建了一个张量流数据集。我们就这样称呼吧labeled_ds。我通过从各自的类目录加载所有图像文件来准备这个数据集。我已经按照这里的教程进行操作:tensorflowguide to load image dataset
现在,我需要分成labeld_ds三个不相交的部分:训练、验证和测试。我正在查看tensorflow API,但没有允许指定分割百分比的示例。我在load 方法中找到了一些东西,但我不知道如何使用它。此外,我怎样才能对分割进行分层?
# labeled_ds contains multi class data, which is unbalanced.
train_ds, val_ds, test_ds = tf.data.Dataset.tfds.load(labeled_ds, split=["train", "validation", "test"])
Run Code Online (Sandbox Code Playgroud)
我被困在这里,非常感谢任何有关如何从这里取得进展的建议。提前致谢。
我是 PyTorch 的新手,我正在探索方法的功能.to()。根据CUDA 张量的文档,我发现可以在 CPU 和 GPU 内存之间传输张量。
# let us run this cell only if CUDA is available
if torch.cuda.is_available():
# creates a LongTensor and transfers it to GPU as torch.cuda.LongTensor
a = torch.full((10,), 3, device=torch.device("cuda"))
# transfers it to CPU, back to being a torch.LongTensor
b = a.to(torch.device("cpu"))
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我想知道是否总是需要将张量从 GPU 传输回 CPU,也许是为了释放 GPU 内存?运行时不会自动清除GPU内存吗?
除了它在 CPU 和 GPU 之间传输数据的用途之外,我还想知道该.to()方法的推荐用法(从内存角度)。提前致谢。
我在 Linux 机器上创建了一个 .tar 文件,如下所示:
tar cvf test.tar test_folder/
Run Code Online (Sandbox Code Playgroud)
其中 test_folder 包含一些文件,如下所示:
test_folder
|___ file1.jpg
|___ file2.jpg
|___ ...
Run Code Online (Sandbox Code Playgroud)
我无法使用 Python 以编程方式提取 tar 存档中的各个文件。更具体地说,我尝试了以下方法:
import tarfile
with tarfile.open('test.tar', 'r:') as tar:
img_file = tar.extractfile('test_folder/file1.jpg')
# img_file contains the object: <ExFileObject name='test_folder/test.tar'>
Run Code Online (Sandbox Code Playgroud)
这里,img_file似乎不包含请求的图像,而是包含源.tar文件。我不确定我在哪里把事情搞砸了。任何建议都会非常有帮助。提前致谢。
我正在使用 tensorflow 2.1 和 python 3.7
以下代码片段用于构建张量流图。当作为独立的 python 脚本执行时,代码运行没有错误。(可能 tensorflow 正在以 Eager 模式运行?我不确定。)
import tensorflow as tf
patches = tf.random.uniform(shape=(1, 10, 50, 300), dtype=tf.dtypes.float32)
s = tf.shape(patches)
patches = [patches[0][x][y] - tf.reduce_mean(patches[0][x][y]) for y in tf.range(s[2]) for x in tf.range(s[1])]
Run Code Online (Sandbox Code Playgroud)
但是,当这是张量流图的一部分时,代码会失败。我收到以下错误:tensorflow。
python.framework.errors_impl.OperatorNotAllowedInGraphError:
tf.Tensor不允许迭代:此函数中禁用了 AutoGraph。尝试直接用@tf.function 装饰它。
我还将装饰器添加@tf.function到包装上述代码行的方法中。它没有帮助。我不确定我是否完全理解用 装饰的含义@tf.function。我还检查了这可能是在张量流图中使用 python 列表理解的问题。我不确定如何使用tf.map_fn或tf.while_loop来处理我的情况,因为我有嵌套循环。
提前致谢!
这是一个说明我的问题的代码片段:
class A {...};
const A& foo1() {...}
const A& foo2() {...}
void foo3(int score) {
if (score > 5)
const A &reward = foo1();
else
const A &reward = foo2();
...
// The 'reward' object is undefined here as it's scope ends within the respective if and else blocks.
}
Run Code Online (Sandbox Code Playgroud)
如何在 if else 块之后访问reward对象foo3()?这是避免代码重复所必需的。
提前致谢 !