我开始学习AnyEvent并且有一些麻烦.我完全误解了如何获得异步利润,fe:
#!/usr/bin/env perl
package LatencySub;
use strict;
use warnings;
use AnyEvent;
# sub for emulate latency - is it right way?
sub do_delay{
my ($name, $delay) = (@_);
my $cv = AE::cv;
my $timer = AE::timer $delay, 0, sub { $cv->send() };
$cv->recv;
return $name.' proceed, delay is '.$delay;
};
package main;
use 5.12.0;
use warnings;
use Smart::Comments;
use AnyEvent;
my @list = (
{ name => 'first', delay => 1 },
{ name => 'second', delay => 1 }, …Run Code Online (Sandbox Code Playgroud) 我正在尝试并行化以下函数(伪代码):
vector<int32> out;
for (int32 i = 0; i < 10; ++i)
{
int32 result = multiplyStuffByTwo(i);
// Push to results
out.push_back(result);
}
Run Code Online (Sandbox Code Playgroud)
当我现在并行化for循环并将push_back部分定义为关键路径时,我遇到的问题是(当然)结果的顺序并不总是正确的.如何使线程运行在for循环的最后一行以正确的顺序执行代码?谢谢!
我正在从这种格式的excel中提取数据
product1 | unnamedcol2 | product2 | unnamedcol4 | product3 | unnamedcol6 | ------------------------------------------------------------------------------- @1foo | 1.10 | @1foo | 0.3 | @1foo | 0.3 @2foo | 1.00 | @2foo | 2 | @2foo | @3foo | 1.52 | @3foo | 2.53 | @3foo | @4foo | 1.47 | | | @4foo | 1.31 @5foo | 1.49 | | | @5foo | 1.31
该文件使用所有255个字段.使用dapper-dot-net我通过此代码获取数据
IEnumerable<IDictionary<string, object>> excelDataRaw =
conn.Query(string.Format("select * from {0}", table)).Cast<IDictionary<string, object>>();
Run Code Online (Sandbox Code Playgroud)
我将这些数据传递给这些测试方法.数据作为IEnumerable of IDictionaries返回,其中每个键都是一个产品,每个值都是一个IDictionary,其中每个键是product列中的值,相应的值是unnamedcol中产品列右侧的值.
var excelDataRefined = …Run Code Online (Sandbox Code Playgroud) 假设我有一个f带整数参数的函数.f可能不会终止某些论点,但其结果同样有价值.(具体来说,参数可以是随机数生成器的种子,它被传递给SAT求解器.)
我想用并发和调用f 1,f 2,f 3等,并在第一个结束返回.因此,每个线程应该运行看起来像的代码
comp <- start_proc (f 1)
wait(comp || anyDone) -- wait for _either_ of these signals to be true
if comp then
set anyDone = True
Run Code Online (Sandbox Code Playgroud)
最简单的方法是什么?我想到了AMB操作员,但我需要同时运行所有进程(例如在24或80核计算机上).(分布式计算解决方案会更好.)对AMB维基页面的肤浅看法表明它可能不支持非终止进程?
目前,我没有得到满足我想要的答案.我认为这可能更像是我创建流程的方式而不是其他任何问题.
限定
runProc (x:xs) =
createProcess (proc x xs) >>= \(_, _, _, h) -> waitForProcess h
Run Code Online (Sandbox Code Playgroud)
然后,我希望赛runProc ["zsh", "-c", "sleep 3"]和runProc ["ls"].我修改了托马斯的回答,但是没有用.
raceL :: [IO ?] -> IO ?
raceL ops …Run Code Online (Sandbox Code Playgroud) 所以我进入并发编程,但由于某种原因我甚至无法使用基础知识.我有一个名为fork.c的文件,其中包含一个方法main.在这个方法主要我分叉两次,进入子进程1和2.
在孩子1中,我打印了'A'字符50次.
在孩子2中,我打印字符'B'50次.
当我运行我的代码时,我得到输出AAAAA ... AAAABBBBBB .... BBBBBB.但从来没有像ABABABABABABAB那样....事实上,有时我甚至得到了BBBBB ...... BBBBAAAA ...... AAAAA.
那么为什么我会遇到这种行为?也许我完全错了.
#include <stdlib.h>
#include <stdio.h>
void my_char(char n) {
write(1, &n, 1);
}
int main() {
int status;
pid_t child1, child2;
if (!(child1 = fork())) {
// first childi
int a;
for (a = 0; a < 50; a++) {
my_char('A');
}
exit(0);
} else if (!(child2 = fork())) {
// second child
int a;
for (a = 0; a < 50; a++) {
my_char('B');
}
exit(0);
} …Run Code Online (Sandbox Code Playgroud) 假设我有一个应该转换为Map的集合,但不是像map方法那样一对一的方式.
var map = collection.mutable.HashMap()
for (p <- dataList.par) {
if(cond1(p)) {
map += (p, true)
} else {
// do nothing
}
}
Run Code Online (Sandbox Code Playgroud)
我想出了几个解决方案,想知道什么是最好的.
map.synchronize { map += (p, true) }
使用actor更新地图.但我不知道如何等待所有演员任务完成
yield Some(p) or None然后跑foreach { case Some(p) => map += (p, true)}.但是,如果第一个迭代器来自并行集合,我不知道如何使它成为顺序.我正在尝试并行化我的Web解析工具,但速度增益似乎非常小.我有i7-2600K(8核超线程).
这里有一些代码可以向您展示这个想法.我只是展示Parallel.ForEach但你明白了:
List<string> AllLinks = this.GetAllLinks();
ConcurrentDictionary<string, Topic> AllTopics = new ConcurrentDictionary<string, Topic> ( );
int count = 0;
Stopwatch sw = new Stopwatch ( );
sw.Start ( );
Parallel.ForEach ( AllLinks, currentLink =>
{
Topic topic = this.ExtractTopicData ( currentLink );
this.AllTopics.TryAdd ( currentLink, topic );
++count;
if ( count > 50 )
{
Console.WriteLine ( sw.ElapsedMilliseconds );
count = 0;
}
} );
Run Code Online (Sandbox Code Playgroud)
我得到这些时间:
Standard foreach loop:
24582
59234
82800
117786
140315
2 links per second …Run Code Online (Sandbox Code Playgroud) 如何实现表单的繁忙旋转机制
while(variable == 0);
Run Code Online (Sandbox Code Playgroud)
在某个事件发生后,其他CUDA线程将变量更新为1.
我试着像上面那样编写它,但代码似乎被忽略了,调用线程只是在没有等待的情况下运行它.我绝对确定该值为0,但线程根本不会等待.另外,如果我写:
while(variable == 0) __threadfence();
Run Code Online (Sandbox Code Playgroud)
为了不让缓存变量的风险,线程无限期地阻塞,甚至认为变量最终被设置为1.这对我来说都是非常奇怪的行为,因为在CPU上复制此代码会产生正确的行为.
编辑:奇怪的是,如果我有每个1个线程的块,这似乎正常工作,但如果我在一个块中有多个线程则不行.因此,来自一个块的线程可以看到来自其他块的线程完成的写入,但不能查看来自同一块的线程完成的写入.奇怪...
我正在尝试使用.NET任务读取使用ProtoBuf.NET序列化的多个文件,如下所示:
public static ResultsDump Amalgamate(RuntimeTypeModel model, IEnumerable<string> files)
{
var readDumpTasks =
files.Select(fn =>
Task<ResultsDump>.Factory.StartNew(() => {
try {
using (var dumpFile = new FileStream(fn, FileMode.Open))
{
var miniDump = (ResultsDump)model.Deserialize(dumpFile, null, typeof(ResultsDump));
if (miniDump == null) {
throw new Exception(string.Format("Failed to deserialize dump file {0}", fn));
}
//readDumps.Add(miniDump);
return miniDump;
}
}
catch (Exception e) {
throw new Exception(string.Format("cannot read dump file {0}: {1}", fn, e.Message), e);
}
})).ToArray();
Task.WaitAll(readDumpTasks);
var allDumps = readDumpTasks.Select(t => t.Result).ToList();
// Goes on.. …Run Code Online (Sandbox Code Playgroud) .net c# parallel-processing protobuf-net task-parallel-library
有人知道如何使用所有内核来计算集成?我需要使用并行化或并行表,但如何?
f[r_] := Sum[(((-1)^n*(2*r - 2*n - 7)!!)/(2^n*n!*(r - 2*n - 1)!))*
x^(r - 2*n - 1), {n, 0, r/2}];
Nw := Transpose[Table[f[j], {i, 1}, {j, 5, 200, 1}]];
X1 = Integrate[Nw . Transpose[Nw], {x, -1, 1}];
Y1 = Integrate[D[Nw, {x, 2}] . Transpose[D[Nw, {x, 2}]], {x, -1, 1}];
X1//MatrixForm
Y1//MatrixForm
Run Code Online (Sandbox Code Playgroud) c# ×3
.net ×2
concurrency ×2
performance ×2
asynchronous ×1
c ×1
c++ ×1
cuda ×1
haskell ×1
linq ×1
loops ×1
openmp ×1
perl ×1
protobuf-net ×1
scala ×1