在 C# 中,有一个System.Threading.Tasks.Parallel.For(...)which 的作用与循环相同for,没有顺序,但在多个线程中。问题是,它只适用于long和int,我想使用ulong。好的,我可以打字,但我在边框方面遇到了一些问题。
比方说,我想要一个从long.MaxValue-10到 的循环long.MaxValue+10(记住,我说的是ulong)。我怎么做?
一个例子:
for (long i = long.MaxValue - 10; i < long.MaxValue; ++i)
{
Console.WriteLine(i);
}
//does the same as
System.Threading.Tasks.Parallel.For(long.MaxValue - 10, long.MaxValue, delegate(long i)
{
Console.WriteLine(i);
});
//except for the order, but theres no equivalent for
long max = long.MaxValue;
for (ulong i = (ulong)max - 10; i < (ulong)max + 10; ++i)
{
Console.WriteLine(i);
}
Run Code Online (Sandbox Code Playgroud) 我有 MPI 库的问题。我必须从文件中读取文本并将其发送到另一个进程,例如作为向量。
我编写了以下代码:
#include "mpi.h"
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
#include <fstream>
#include <cstring>
#include <vector>
class PatternAndText
{
public:
static std::string textPreparaation()
{
std::ifstream t("file.txt");
std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
std::string text = str;
return text;
}
};
int main(int argc, char* argv[])
{
int size, rank ;
std::string text;
std::vector<char> cstr;
MPI_Init(&argc, &argv);
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0)
{
text = PatternAndText::textPreparaation();
std::vector<char> cstr(text.c_str(), text.c_str() + text.size() + 1);
}
MPI_Bcast(cstr.data(), cstr.size(), MPI_CHAR,0,MPI_COMM_WORLD);
if …Run Code Online (Sandbox Code Playgroud) 我想使用 parLapply 并且我正在设置我的代码,就像这里介绍的那样:http : //www.win-vector.com/blog/2016/01/parallel-computing-in-r/
最后几次效果很好。但是,在我当前的parLapply通话中,我收到了错误消息
Error in checkForRemoteErrors(val) : 3 nodes produced errors; first error: could not find function "bindToEnv"。
这里有一个简短的例子:
#' Copy arguments into env and re-bind any function's lexical scope to bindTargetEnv .
#'
#' See http://winvector.github.io/Parallel/PExample.html for example use.
#'
#'
#' Used to send data along with a function in situations such as parallel execution
#' (when the global environment would not be available). Typically called within
#' a function that …Run Code Online (Sandbox Code Playgroud) 我正在对 5 亿个文件运行以下 MD5 检查以检查重复项。脚本需要永远运行,我想知道如何加快速度。我怎么能加快速度?当散列已经存在时,我可以使用 try catch 循环而不是 contains 来抛出错误吗?大家会推荐什么?
$folder = Read-Host -Prompt 'Enter a folder path'
$hash = @{}
$lineCheck = 0
Get-ChildItem $folder -Recurse | where {! $_.PSIsContainer} | ForEach-Object {
$lineCheck++
Write-Host $lineCheck
$tempMD5 = (Get-FileHash -LiteralPath $_.FullName -Algorithm MD5).Hash;
if(! $hash.Contains($tempMD5)){
$hash.Add($tempMD5,$_.FullName)
}
else{
Remove-Item -literalPath $_.fullname;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Jupyter Notebook 并行化一个简单的 python 循环。我尝试使用,Pool但它永远挂起,我必须杀死笔记本才能阻止它。
def process_frame(f):
new_dict = dict()
pc_dict = calculate_area(fl)
for key in pc_dict:
if key not in new_dict:
new_dict[key] = 0
new_dict[key] = float(sum(pc_dict[key]))
full_pc_dict[fl] = new_dict
Run Code Online (Sandbox Code Playgroud)
frames_list = [0, 1, 2, 3, 4, 5, 6]
我想process_frame为frames_list.
请注意,最终结果应该是一个包含来自process_frame. 我不知道将它附加在函数的末尾是否是个好主意。
关于如何使用 Jupyter Notebook 执行此操作的任何建议?另外,是否可以tqdm使用这种并行处理?
亲切的问候
我的代码:
#include <cstdio>
#include "omp.h"
int main() {
omp_set_num_threads(4);
#pragma omp parallel
{
#pragma omp parallel for
for (int i = 0; i < 6; i++)
{
printf("i = %d, I am Thread %d\n", i, omp_get_thread_num());
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出:
i = 0, I am Thread 0
i = 1, I am Thread 0
i = 2, I am Thread 0
i = 0, I am Thread 0
i = 1, I am Thread 0
i = 0, …Run Code Online (Sandbox Code Playgroud) I have the following code:
public class CheckIfSame implements Runnable {
private int[][] m;
private int[][] mNew;
private int row;
private boolean same;
public CheckIfSame(int[][] m,int[][] mNew,,int row,boolean same) {
this.m = m;
this.mNew = mNew;
this.row = row;
this.same = same;
}
@Override
public void run() {
for (int i = 0; i < mUpd[0].length; i++) {
if(m[row][i] != mUpd[row][i]) {
same = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
Basically, the idea of this is that i use multi threading …
我写了这个简单的测试程序:
fun main() {
println("Main Start")
thread {
println("Thread Start")
Thread.sleep(3000)
println("Thread End")
}
println("Main End")
}
Run Code Online (Sandbox Code Playgroud)
正如我所看到的,输出是:
Main Start
Main End
Thread Start
Thread End
Run Code Online (Sandbox Code Playgroud)
我的期望是至少不会打印“线程结束”消息,因为主函数已结束并且该主线程应该完成运行。
Kotlin 进程是否总是在完成之前等待线程完成?
请注意:这只是出于好奇而提出的问题,与编写更好的多线程代码无关。当然,我不会也不会在实际项目中编写这样的代码。
添加关键字时可能会发生内联替换inline。所以我很好奇。
假设我们有这样的代码:
static bool done = false;
inline void setDone(bool newState) {
done = newState;
}
inline bool getDone() {
return done;
}
void someWorkerThreadJob() {
// Accessing without any lock/atomic/volatile
while (getDone() == false) {
}
}
Run Code Online (Sandbox Code Playgroud)
可以someWorkerThreadJob()像下面这样编译并进入无限循环吗?
void someThreadJob() {
while (done == false) {
}
}
Run Code Online (Sandbox Code Playgroud)
这也将我引向了下一个问题。类中的getter和setter怎么样?在类中定义的成员函数是隐式的inline,所以我认为可能会发生内联替换,因此同样的问题。这样对吗?
我读过在 R 中执行嵌套 foreach 循环的正确方法是通过嵌套运算符%:%(例如https://cran.r-project.org/web/packages/foreach/vignettes/nested.html)。
但是,使用这种方法时,不能在内循环和外循环之间添加代码——请参见下面的示例。
有没有办法创建嵌套的、并行的 foreach 循环,以便可以在内循环和外循环之间添加代码?
更一般地说,我想到的显而易见的方法有什么问题,即简单地用%dopar%运算符而不是%:%运算符嵌套两个 foreach 循环?请参阅下面的简单示例。
library(foreach)
# Set up backend
cl = makeCluster(6)
registerDoParallel(cl)
on.exit(stopCluster(cl))
# Run nested loop with '%:%' operator. Breaks if adding code between the inner and outer loops
foreach(i=1:2) %:%
# a = 1 #trivial example of running code between outer and inner loop -- throws error
foreach(j = 1:3) %dopar% {
i * j
}
# Run nested loop using …Run Code Online (Sandbox Code Playgroud)