Пример #1
0
int main(int argc, char** argv)
{

    struct timeval start, end;
    
    int i,j,k;
	double totaltime;
	
	// Check input arguments
	if (argc < 5)
	{
		printf("Error in arguments! Three arguments required: graph filename, N, threshold and d\n");
		return 0;
	}

	// get arguments 
	char filename[256];
	strcpy(filename, argv[1]);
	N = atoi(argv[2]);
	threshold = atof(argv[3]);
	d = atof(argv[4]); 
	num_threads = atoi(argv[5]);

	// Check input arguments
	if ((num_threads < 1) || (num_threads > maxThreads)) 
	{
		printf("Threads number must be >= 1 and  <= %d!\n", maxThreads);
		exit(1);
	}

	Threads_Allocation();
	Nodes_Allocation();
	
	// OR read probabilities from files
    Read_from_txt_file(filename);
	//Read_P_from_txt_file();
	//Read_E_from_txt_file();

    Random_P_E();

    printf("\n");

    printf("Parallel version of Pagerank\n");

    gettimeofday(&start, NULL);
    Pagerank();
	gettimeofday(&end, NULL);  

    /*for (i = 0; i < N; i++)
    {
        printf("P_t1[%d] = %f\n",i, Nodes[i].p_t1);
    }
    
    printf("\n");*/
    
	// Print no of iterations
    printf("Total iterations: %d\n", iterations);


    totaltime = (((end.tv_usec - start.tv_usec) / 1.0e6+ end.tv_sec - start.tv_sec) * 1000) / 1000;

	printf("\nTotaltime = %f seconds\n", totaltime);
    
    printf("End of program!\n");
    
    return (EXIT_SUCCESS);
}
Пример #2
0
int main(int argc, char** argv)
{
	// Check input arguments
	if (argc < 5)
	{
		printf("Error in arguments! Three arguments required: graph filename, N, threshold and d\n");
		return 0;
	}

	// get arguments 
	char filename[256];
	strcpy(filename, argv[1]);
	N = atoi(argv[2]);
	threshold = atof(argv[3]);
	d = atof(argv[4]);
    
    int i, j, k;
	double totaltime;

	// a constant value contributed of all nodes with connectivity = 0
	// it's going to be addes to all node's new probability
	double sum = 0;
    
	// Allocate memory for N nodes
    Nodes = (Node*) malloc(N * sizeof(Node));
    
    for (i = 0; i < N; i++)
	{
		Nodes[i].con_size = 0;
        Nodes[i].To_id = (int*) malloc(sizeof(int));
    }

    Read_from_txt_file(filename);
    
	// set random probabilities
    Random_P_E();
	// OR read probabilities from files
	//Read_P_from_txt_file();
	//Read_E_from_txt_file();
    
	gettimeofday(&start, NULL);
    
	
    /********** Start of algorithm **********/
    
    // Iterations counter
    int iterations = 0;
    int index;
    
    // Or any value > threshold
    double max_error = 1;
    
    printf("\nSerial version of Pagerank\n");

    // Continue if we don't have convergence yet
    while (max_error > threshold)
    {
    	sum = 0;
		
		// Initialize P(t) and P(t + 1) values
        for (i = 0; i < N; i++)
        {
            // Update the "old" P table with the new one 
            Nodes[i].p_t0 = Nodes[i].p_t1;  
            Nodes[i].p_t1 = 0;
        }

        // Find P for each webpage
        for (i = 0; i < N; i++)
        {
            
            if (Nodes[i].con_size != 0)
            {
                
                // Compute the total probability, contributed by node's neighbors
                for (j = 0; j < Nodes[i].con_size; j++)
                {
					index = Nodes[i].To_id[j];	
					Nodes[index].p_t1 = Nodes[index].p_t1 + (double) Nodes[i].p_t0 / Nodes[i].con_size;
				}

            }
            
            else
            {
				// Contribute to all
                sum = sum + (double)Nodes[i].p_t0 / N;
            }
            
        }
        
		max_error = -1;
        
		// Compute the new probabilities and find maximum error
        for (i = 0;i < N; i++)
        {
            Nodes[i].p_t1 = d * (Nodes[i].p_t1 + sum) + (1 - d) * Nodes[i].e;
            
            if (fabs(Nodes[i].p_t1 - Nodes[i].p_t0) > max_error)
            {
                max_error = fabs(Nodes[i].p_t1 - Nodes[i].p_t0);
            }
            
        }
        
        printf("Max Error in iteration %d = %f\n", iterations+1, max_error);
        iterations++;
    }

	gettimeofday(&end, NULL);
    
	printf("\n");
  
	// Print final probabilitities
    /*for (i = 0; i < N; i++)
    {
        printf("P_t1[%d] = %f\n",i,Nodes[i].p_t1);
    }
    
    printf("\n");*/
    
    // Print no of iterations
    printf("Total iterations: %d\n", iterations);
	
    totaltime = (((end.tv_usec - start.tv_usec) / 1.0e6 + end.tv_sec - start.tv_sec) * 1000) / 1000;

	printf("\nTotaltime = %f seconds\n", totaltime);
    
    printf("End of program!\n");
    
    return (EXIT_SUCCESS);
}