我正在为使用并行线程进行计算的推荐引擎编写代码。在运行时,我在 8 核 CPU 上获得了 265-300% 的最大 CPU 使用率(使用 top 命令)。我没有得到的是为什么即使将近 50% 的 CPU 空闲,它也没有使用完整的 CPU。使用并行的伪代码部分是:
getRecoFromCandidates(){
t=new Thread(new KNN(uid,profile,candidates));// does knn using closest neighbors
rec=RecommendationsFromSet(NB_RECOMMENDATIONS,uid,candidatesI,val);// finds recommendations
return rec;
}
RecommendationsFromSet() {
Thread worker = new Thread(new storiesClickProcess(nbRec,candidatesI,likedSet));
worker.start();
threads.add(worker);
}
int running = 0;
do {
running = 0;
for (Thread thread : threads) {
if (thread.isAlive()) {
running++;
}
}
} while (running > 0);
if(val==1){
List<Story> list1 = new ArrayList<Story>();
for(Integer sid: storiesClicks.keySet()){
list1.add(new Story(sid,storiesClicks.get(sid)));
}
Collections.sort(list1,new StoriesList());
for(i=0;i<nbRec;i++){
rec.add(list1.get(i).getSid());
}
}
return rec;
}
parGetClosestNeighbor() {
Thread worker = new Thread(new formNetworkProcess(nbFriends,networkScore[i],profile,candidatesI,candidatesISet) );
worker.start();
threads.add(worker);
}
int running = 0;
do {
running = 0;
for (Thread thread : threads) {
if (thread.isAlive()) {
running++;
}
}
} while (running > 0);
List<Network> list = new ArrayList<Network>();
for(i=0;i<TASK_NUM;i++){
for(Integer sid: networkScore[i].keySet()){
list.add(new Network(sid,networkScore[i].get(sid)));
}
}
Collections.sort(list,new NetworkList());
for(i=0;i<nbFriends;i++){
friends.add(list.get(i).getUid());
}
synchronized(network.get(uid)) {
network.get(uid).clear();
for( i=0;i<friends.size();i++){
network.get(uid).add(friends.get(i));
}
}
return friends;
}