考虑以下张量流代码片段:
import time
import numpy as np
import tensorflow as tf
def fn(i):
# do some junk work
for _ in range(100):
i ** 2
return i
n = 1000
n_jobs = 8
stuff = np.arange(1, n + 1)
eager = False
t0 = time.time()
if eager:
tf.enable_eager_execution()
res = tf.map_fn(fn, stuff, parallel_iterations=n_jobs)
if not eager:
with tf.Session() as sess:
res = sess.run(res)
print(sum(res))
else:
print(sum(res))
dt = time.time() - t0
print("(eager=%s) Took %ims" % (eager, dt * 1000))
Run Code Online (Sandbox Code Playgroud)
如果使用 …
我想并行化一个 for 循环,其中更新 unordered_map 的值:
unordered_map<string,double> umap {{"foo", 0}, {"bar", 0}};
#pragma omp parallel for reduction(my_reduction:umap)
for (int i = 0; i < 100; ++i)
{
// some_string(i) would return either "foo" or "bar"
umap[some_string(i)] += some_double(i);
}
Run Code Online (Sandbox Code Playgroud)
因此,unordered_map 中不会创建新条目,只会更新现有条目的总和。
在这个答案中,用户声明的归约是针对向量的情况定义的。在 unordered_map 的情况下,用户声明的归约是否可以类似地定义?
我正在读取一个视频文件,每 20 帧我将第一帧存储在输入队列中。一旦我在输入队列中获得了所有必需的帧,我就会运行多个进程来对这些帧执行一些操作并将结果存储在输出队列中。但代码总是卡在 join 处,我尝试了针对此类问题提出的不同解决方案,但似乎都不起作用。
import numpy as np
import cv2
import timeit
import face_recognition
from multiprocessing import Process, Queue, Pool
import multiprocessing
import os
s = timeit.default_timer()
def alternative_process_target_func(input_queue, output_queue):
while not output_queue.full():
frame_no, small_frame, face_loc = input_queue.get()
print('Frame_no: ', frame_no, 'Process ID: ', os.getpid(), '----', multiprocessing.current_process())
#canny_frame(frame_no, small_frame, face_loc)
#I am just storing frame no for now but will perform something else later
output_queue.put((frame_no, frame_no))
if output_queue.full():
print('Its Full ---------------------------------------------------------------------------------------')
else:
print('Not Full')
print(timeit.default_timer() - s, ' seconds.') …Run Code Online (Sandbox Code Playgroud) 我的场景是这样的。
我能想到的一个初步解决方案是增加 max-open-requests 设置。但这里的问题是我不知道需要提前发送多少报告。
有人可以建议一个替代解决方案,例如限制通过 Futures.traverse 发生的并行性
据我所知,水晶会通过 io 循环光纤,这意味着如果一根光纤正在等待 io,水晶将切换到另一根光纤。
如果我们生成两个纤程,但其中一个在没有 io 的情况下进行持续计算/循环,该怎么办?
例如,使用下面的代码,服务器不会响应任何 http 请求
spawn do
Kemal.run
end
spawn do
# constant computation/loop with no IO
some_func
end
Fiber.yield
# or sleep
Run Code Online (Sandbox Code Playgroud) 我想用一个#define标志来控制是否使用 openmp。由于#pragma不能不在 a 内#define,所以我尝试了
#define USE_OPENMP // Toggle this on/off
#ifdef USE_OPENMP
#define OMP_FOR(n) __pragma("omp parallel for if(n>10)")
#else
#define OMP_FOR(n) // do nothing
#endif
Run Code Online (Sandbox Code Playgroud)
然后在我的代码中我可以:
int size_of_the_loop = 11;
OMP_FOR(size_of_the_loop) // activate openmp if(n>10)
for(){
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
我对#define相关的东西不熟悉,想知道是否可以实现这一目标?谢谢。
我想在集群上运行作业。不同节点上有不同数量的 CPU,我不知道哪些节点将分配给我。正确的选项是什么,以便作业可以在所有节点上创建与 CPU 一样多的任务?
#!/bin/bash -l
#SBATCH -p normal
#SBATCH -N 4
#SBATCH -t 96:00:00
srun -n 128 ./run
Run Code Online (Sandbox Code Playgroud) 我目前正在使用 future 包进行并行化,如下所示:
plan(multisession, gc = TRUE)
standardised_addresses1 <- future_lapply(1:20000, function(x) x*x)
Run Code Online (Sandbox Code Playgroud)
问题是它使用了服务器上的所有 CPU。我想通过设置如下参数来限制使用的CPU数量:workers = 18
看起来使用torch.nn.DataParallel改变了输出大小。尽管在官方文档https://pytorch.org/docs/stable/nn.html#torch.nn.DataParallel中
,有关大小更改的所有信息如下:
当模块在forward()中返回一个标量(即0维张量)时,该包装器将返回一个长度等于数据并行中使用的设备数量的向量,其中包含每个设备的结果。
我的模块返回 10 个坐标的张量,并且我有 2 个 GPU,我想在其中运行代码。我的 CNN 的最后一层是nn.Linear(500, 10).
import torch
import torch.nn as nn
net = LeNet() #CNN-class written above
device = torch.device("cuda:0")
net.to(device)
net = nn.DataParallel(net)
#skipped some code, where inputs and targets are loaded from files
output = net(input)
criterion = nn.SmoothL1Loss()
loss = criterion(output, target)
Run Code Online (Sandbox Code Playgroud)
请注意,不调用DataParallel这段代码也可以正常工作。DataParallel当尝试计算损失时会发生运行时错误。
RuntimeError: The size of tensor a (20) must match the size of tensor b (10) at non-singleton …Run Code Online (Sandbox Code Playgroud) 阅读[algorithms.parallel.exec]p2上最新的 C++20 草案,我发现了 C++17 中没有的一段:
\n\n\n\n\n如果某个对象被元素访问函数修改,则该算法将不会对该对象执行其他非同步访问。修改元素访问函数是那些被指定为修改对象的函数。[注意:例如,
\nswap、++、--、@=和 赋值修改对象。对于赋值和@=运算符,仅修改左侧参数。\xe2\x80\x94尾注]
这究竟保证了什么?为什么添加它?
\n\n例如,这是否保证“并行”算法在算法的整个执行过程中不会将迭代器递增两次?(或者,如果是的话,它将以同步/顺序的方式)?
\n