这里的程序用的公式是老版的,
PR_Temp[i] = (1 - Alpha) + Alpha*(sum);
没有除以N。
没有除以N。
Google后来调整时使用了(1 - Alpha)/N,公式的其他部分未作任何变动.
先生成需要的数据:
</pre><p></p><p style="font-family:'Microsoft YaHei',SimSun,Verdana,Arial,Helvetica,sans-serif; line-height:21px; font-size:14px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.2000007629395px">再来看看原始的C++代码:</span></span></p><p style="font-family:'Microsoft YaHei',SimSun,Verdana,Arial,Helvetica,sans-serif; line-height:21px; font-size:14px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.2000007629395px"></span></span></p><p style="font-family:'Microsoft YaHei',SimSun,Verdana,Arial,Helvetica,sans-serif; line-height:21px; font-size:14px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.2000007629395px"></span></span></p><pre name="code" class="cpp">#include<stdio.h>
#include<time.h>
#include<iostream>
#include<fstream>
using namespace std;
#define numberOfVertex 500
#define Max_Iteration_Number 10000
#define Alpha 0.85
#define InitPageRankValue 6
#define END_WEIGHT 1e-7
//END condition: when the PR value stable
bool END(float a[], float b[])
{
float sum = 0;
for (int i = 0; i < numberOfVertex ; ++i)
{
sum += abs(a[i] - b[i]);
}
cout << sum << endl;
if (sum < END_WEIGHT)
{
return true;
}
return false;
}
void PageRank(float *Graph, float PR[])
{
//Display the Graph:
/*for (int i = 0; i < numberOfVertex; ++i)
{
for (int j = 0; j < numberOfVertex; ++j)
{
printf("%f\t", *(Graph +i*numberOfVertex +j));
}
printf("\n");
}
*/
Calculate the sum of out-degree of every vertex
eg. the sum of every line
clock_t begin, end;
float PR_Temp[numberOfVertex ];
begin = clock();
int iter = 0; //迭代次数
for (int m = 0; m < Max_Iteration_Number; ++m)
{
iter++;
float sumOfOutDegree[numberOfVertex ];
for (int i = 0; i < numberOfVertex ; ++i) //初始化
{
sumOfOutDegree[i] = 0.0;
}
//Calculate the sum of degree of each vertex
for (int i = 0; i < numberOfVertex ; ++i)
{
float sum = 0;
for (int j = 0; j < numberOfVertex ; ++j)
{
sum += *(Graph +i*numberOfVertex +j);
}
sumOfOutDegree[i] = sum;
}
//Calculate the PR value of every vertex.
for (int i = 0; i < numberOfVertex ; ++i)
{
float sum = 0;
int k = 0;
for (int j = i; j < numberOfVertex *numberOfVertex ;
j += numberOfVertex )
{
if (*(Graph + j) == 1)
{
if(sumOfOutDegree[k] != 0)
sum += PR[k] / sumOfOutDegree[k];
}
k++;
//printf("%f\n", sum);
}
PR_Temp[i] = (1 - Alpha) + Alpha*(sum);
}
if (END(PR_Temp, PR))
{
break;
}
else{
for (int i = 0; i < numberOfVertex ; ++i)
{
PR[i] = PR_Temp[i];
}
}
}
end = clock();
printf("Calculate %d iteration of PageRank value cost us:%d ms.\n",
iter, end - begin);
}
int main(){
float arc[500][500];
for (int i=0; i<numberOfVertex; i++) //初始化邻接矩阵
for (int j=0; j<numberOfVertex; j++)
arc[i][j]=0;
float PR[500];
for(int n=0;n<500;n++)
PR[n] = 1.0/500;
ifstream fin("Graph.txt");
int k,l;
while(fin>>k>>l)
{
arc[k][l] = 1;
}
float (*Graph)[500]=arc;
PageRank(*Graph,PR);
fin.close(); //读取完毕后,关闭文件
}
该函数在main中首先根据文件建立了一个邻接矩阵。
for (int j = i; j < numberOfVertex *numberOfVertex ;
j += numberOfVertex )
{
if (*(Graph + j) == 1)
{
if(sumOfOutDegree[k] != 0)
sum += PR[k] / sumOfOutDegree[k];
}
k++;
//printf("%f\n", sum);
}
关于这段代码
int k = 0;
for (int j = i; j < numberOfVertex *numberOfVertex ;
j += numberOfVertex )
{
if (*(Graph + j) == 1)
{
if(sumOfOutDegree[k] != 0)
sum += PR[k] / sumOfOutDegree[k];
}
k++;
//printf("%f\n", sum);
}
看下图就一目了然了:
这个表表示一个邻接矩阵,如果存在链接 u->v ,则该点为1.
现在我们想统计第i个节点的PR值。比如第2个节点的PR值,所用到的该邻接矩阵中的应该是所有指向该节点的值的sum, 也就是绿色圈出来的那一列。。刚开始 *(Graph + j) 是第一行中个绿色圈中的那一个1. 然后j加了numberOfVertex 就跳到第二行了。
好了,再看CUDA 的,现在CUDA优化的思想很简单,在CPU上,我们使用1个线程,串行的计算每个节点的PR值,在上面的代码中,我们也能看到,这个串行算法的时间复杂度为O(n^2)。使用CUDA,我们可以使用与节点数量相同的线程,用一个线程计算一个节点的PR值。
所以CUDA的函数分为两部分,首先在单个线程上计算单个节点的出度数
然后,在单个线程上,计算单个节点的PR值:
代码略