在我的代码中,我有一个服务器进程反复探测传入的消息,它有两种类型.每个进程将发送一次这两种类型,以向服务器进程提供有关其终止的提示.
我想知道使用MPI_Broadcast广播这些终止消息并使用MPI_Probe来探测其到达是否有效.
我尝试使用这种组合,但失败了.这种失败可能是由其他一些事情引起的.所以我想任何知道此事的人都要确认一下.
我是新来的,而且我在编程方面也相对较新.我用C编写了一个程序,我需要使用pthread来加速它.我尝试使用OpenMP这样做,但我不知道如何调试它.此外,我需要找出程序是否使用pthreads和时间更快,但我不知道如何在我的代码中写这个.这是我的代码
enter code here
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#define NTHREADS 2
#define FYLLO(komvos) ((komvos) * 2 + 1)
long factorial(long);
void heap_function (int [], int, int );
void make_heap(long [], int );
void pop_heap(long [], int );
struct thread_data
{
long int n;
long int k;
long *b;
};
main()
{
long int n,k,c,fact=1;
long *a,*b,*d,p[k];
int i,j,rc;
int q[]={2,3,4,5,6,7,8,9,12,13,14,15,16};
pthread_t thread[NTHREADS];
struct thread_data threada;
for(i=0;i<NTHREADS;i++)
{
threada.n=n;
threada.k=k;
threada.b=b;
pthread_create (&thread[i], NULL, (void *)&threada);
} …Run Code Online (Sandbox Code Playgroud) c parallel-processing performance pthreads serial-processing
众所周知,R不是运行大型分析的最有效平台.如果我有一个包含三个参数的大型数据框:
GROUP X Y
A 1 2
A 2 2
A 2 3
...
B 1 1
B 2 3
B 1 4
...
millions of rows
Run Code Online (Sandbox Code Playgroud)
我想在每个组上运行计算(例如,在X,Y上计算Pearson的r)并将结果存储在一个新的数据框中,我可以这样做:
df = loadDataFrameFrom( someFile )
results = data.frame()
for ( g in unique( df$GROUP)) ){
gdf <- subset( df, df$GROUP == g )
partialRes <- slowStuff( gdf$X,gdf$Y )
results = rbind( results, data.frame( GROUP = g, RES = partialRes ) )
}
// results contains all the results here.
useResults(results)
Run Code Online (Sandbox Code Playgroud)
显而易见的问题是,即使在强大的多核机器上,这也非常慢.
我的问题是:是否可以并行化这种计算,例如为每个组或一组组创建一个单独的线程?是否有一个干净的R模式来解决这个简单的除法和问题? …
我面临以下问题:
从初始集[1,2,3,4]计算所有可能的子集,即[[1],[2],[3],[4],[1,2],[1,3],[1,4],[2,3],[2,4],[3,4],[1,2,3],[1,2,4],[1,3,4],[2,3,4],[1,2,3,4]]
我编写了以下Haskell程序generate.hs,这是正确的.
generateSets :: Eq a => [a] -> [[a]] -> [[a]] -> [[a]]
generateSets [] _ _ = []
generateSets src [] _ = let isets = growthup [] src in generateSets src iset iset
generateSets src sets rsets = if null sets' then rsets else generateSets src sets' (rsets++sets')
where sets' = concatMap (flip growthup src) sets
growthup :: (Eq a) => [a] -> [a] -> [[a]]
growthup ps ss = map …Run Code Online (Sandbox Code Playgroud) parallel-processing garbage-collection haskell set lazy-evaluation
我在Java中编写多线程算法时遇到了一些问题.这是我得到的:
public class NNDFS implements NDFS {
//Array of all worker threads
private Thread[] threadArray;
//Concurrent HashMap containing a mapping of graph-states and
//algorithm specific state objects (NDFSState)
private ConcurrentHashMap<State, NDFSState> stateStore;
//Whether the algorithm is done and whether a cycle is found
private volatile boolean done;
private volatile boolean cycleFound;
/**
Constructor that creates the threads, each with their own graph
@param file The file from which we can create the graph
@param stateStore Mapping between graph-states and state …Run Code Online (Sandbox Code Playgroud) 我有以下数据框:
id<-c(1,2,3,4)
date<-c("23-01-08","01-11-07","30-11-07","17-12-07")
df<-data.frame(id,date)
df$date2<-as.Date(as.character(df$date), format = "%d-%m-%y")
Run Code Online (Sandbox Code Playgroud)
在我的表格的第四列中,我想根据日期将我的数据划分为校准和有效,以便在日期<= 2007-12-16的情况下,第四列应该calib是否应该是valid
我写了以下几行:
for ( i in 1:4)
if (df[i,3]<=2007-12-16)(df[i,4]="calib")else (df[i,4]="valid")
Run Code Online (Sandbox Code Playgroud)
第一个问题是,通过执行此命令,第4列中的所有单元格都将变为valid,并且似乎无法正确处理日期条件.所以我的第一个问题是如何解决这个问题.
第二个问题是我的真实数据框有600000行,执行这个命令需要几个小时.我想知道是否有任何方法可以更快地执行此命令并具有完整的CPU容量.
谢谢!
我有两个.c文件(main.c和support.c).首先编译Support.c,然后编译main.c并与support.o链接.我在support.c中有几个非静态全局变量.
如何存储来自support.c的全局变量?如果main.c是多线程的并且有两个线程调用support.c中的函数,它们是否共享这些全局变量,或者它们各自都有自己的副本?
我试图使用foreach和将以下代码转换为并行%dopar%.
library(doSNOW)
library(foreach)
cl<- makeCluster(4, type = "SOCK")
registerDoSNOW(cl)
min_subid <- c()
max_subid <- c()
p_typ <- c()
p_nm <- c()
st_tm<-c()
end_tm <- c()
supp <- c()
chart_type <- c()
foreach(j =1:noOfPhases) %dopar%
{
start_time <-phases[j, colnames(phases)=="StartTime"]
end_time <-phases[j, colnames(phases)=="StopTime"]
phase_type <-phases[j, colnames(phases)=="Phase_Type_Id"]
phase_name <-phases[j, colnames(phases)=="Phase_Name"]
suppress <-phases[j, colnames(phases)=="Suppression_Time"]
chart_typ <-phases[j, colnames(phases)=="chartType"]
conft<-(masterData$Time.Subgroup>=start_time & masterData$Time.Subgroup<=end_time)
masterData[which(conft), colnames(masterData)=="Phase_Type"]<-phase_type
masterData[which(conft), colnames(masterData)=="Phase_Name"]<-phase_name
min_subid <- rbind(min_subid, min(which(conft)))
max_subid <- rbind(max_subid, max(which(conft)))
p_typ <- rbind( p_typ, masterData$Phase_Type[min(which(conft))])
p_nm <- rbind( …Run Code Online (Sandbox Code Playgroud) 在学校,我们上周开始进行多线程处理,现在我们已经进行了多处理,我有点迷失了,所以我将向你解释这个问题.对于练习,我们必须制作一个模拟10000个游戏的赌场游戏模拟器,以便我们知道赌场赢得游戏的频率.所以我编写了模拟器,我有5种方法来运行游戏:
static void game(Croupier croupier)
{
croupier.createNewCardDeck();
croupier.shuffleCards();
croupier.giveCardsToPlayers();
croupier.countPlayerPoints();
croupier.displayResults();
}
Run Code Online (Sandbox Code Playgroud)
如果我在10000次迭代的经典for循环中调用游戏,则运行正常,大约需要2秒,并且银行赢得50%的次数.
如果我使用Parallel.For,它会在shuffleCards上崩溃,因为(我认为)多个进程正在尝试同时编辑同一包卡.
我的第一个想法是将Mutex放在我的shuffleCards上,但是当使用并行编程来提高速度时,它会减慢模拟速度.所以我想把不同进程的数据分开(所以我不是10000次迭代,而是在4个进程上进行2500次,每个循环都有自己的副主持人,玩家,卡等...)
您认为解决此问题的最佳方法是什么?您是否有任何简单的教程解释如何处理使用相同数据的并行工作?你会选择哪种解决方案?谢谢
编辑:ShuffleCard方法
List<Card> randomList = new List<Card>();
Random r = new Random();
int randomIndex = 0;
while (_cards.Count > 0)
{
randomIndex = r.Next(0, _cards.Count); //Choose a random object in the list
randomList.Add(_cards[randomIndex]); //add it to the new, random list
_cards.RemoveAt(randomIndex); //remove to avoid duplicates
}
return randomList;
Run Code Online (Sandbox Code Playgroud)
所以是_cards是croupier的私有财产(调用this._cards = shuffleCards(),每个进程都有相同的卡列表