-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
142 lines (125 loc) · 5.99 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "recommendations_gold.h"
#include "recommendations_kernel.h"
#include "misc_utls.h"
int main(int argc, char *argv[]) {
// read input and construct user rating matrix
if (argc != 6) {
cout << "Usage: parallel-recommenders <path-to-rating-file> <path-to-movie-file> "
"<delimiter> <path-to-input-user-ids> <n>" << endl;
exit(1);
}
string ratingsFileName = argv[1];
string moviesFileName = argv[2];
char *delim = argv[3];
string userIdsFileName = argv[4];
auto n = (unsigned int) strtol(argv[5], nullptr, 10);
RatingsMatrixCSR *ratingMatrix;
try {
ratingMatrix = readInputRatings(ratingsFileName, delim);
} catch (invalid_argument &err) {
cout << err.what() << ratingsFileName << endl;
exit(2);
}
map<unsigned int, string> movieIdNameMapping;
vector<unsigned int> movieIds;
try {
movieIdNameMapping = readInputMovies(moviesFileName, delim);
} catch (invalid_argument &err) {
cout << err.what() << ratingsFileName << endl;
exit(2);
}
vector<unsigned int> inputUserIds;
try {
inputUserIds = readInputUserIds(userIdsFileName);
} catch (invalid_argument &err) {
cout << err.what() << userIdsFileName << endl;
exit(2);
}
movieIds = getMovieIds(movieIdNameMapping);
float totalGold = 0.0;
float totalKernel = 0.0;
float simTime = 0.0;
float simTime2 = 0.0;
float recTime = 0.0;
float recTime2 = 0.0;
Timer timer{};
startTime(&timer);
normalizeRatingVectors(*ratingMatrix);
stopTime(&timer);
cout << "Normalizing Ratings took " << elapsedTime(timer) << " seconds." << endl;
// compute similarity in sequential version (gold)
cout << "Computing UU Similarity - Gold" << endl;
startTime(&timer);
SimilarityMatrix similarityMatrix = computeSimilarityGold(*ratingMatrix);
stopTime(&timer);
simTime = elapsedTime(timer);
cout << "Took " << setprecision(6) << simTime << " seconds." << endl << endl;
totalGold += simTime;
// recommend top n for given user ids in sequential version (gold)
cout << "Calculating Top-" << n << " Recommendations for " << inputUserIds.size() << " users - Gold." << endl;
vector<vector<ItemRating>> userRecommendations;
startTime(&timer);
for (unsigned int inputUserId : inputUserIds) {
vector<ItemRating> recommendations = calculateTopNRecommendationsForUserGold(*ratingMatrix,
similarityMatrix,
movieIds,
inputUserId - 1, n);
userRecommendations.push_back(recommendations);
}
stopTime(&timer);
recTime = elapsedTime(timer);
for (unsigned int i = 0; i < inputUserIds.size(); i++)
storeRecommendationsToFile(userRecommendations[i], movieIdNameMapping,
"goldRecommendations_" + to_string(inputUserIds[i]) + ".csv");
cout << "Took " << setprecision(6) << recTime
<< " seconds for " << inputUserIds.size() << " users." << endl << endl;
totalGold +=recTime;
// Compute similarity in parallel version (kernel)
startTime(&timer);
cout << endl << "Computing UU Similarity - Parallel" << endl;
//define CUDA device memory ptrs
unsigned int *csrRowPtr_d;
unsigned int *csrColIdx_d;
float *csrData_d;
float *userEuclideanNorm_d;
unsigned int dim = ratingMatrix->rowPtrs.size() - 1;
//allocate
allocateMemoryToDevicePtrs(dim, &csrRowPtr_d, &csrColIdx_d, &csrData_d, &userEuclideanNorm_d, *ratingMatrix);
//copy
copyRatingsMatrixToDevicePtrs(dim, csrRowPtr_d, csrColIdx_d, csrData_d, userEuclideanNorm_d, *ratingMatrix);
//similarities
SimilarityMatrix similarityMatrixKernel = computeSimilarityParallel(dim, csrRowPtr_d, csrColIdx_d, csrData_d, userEuclideanNorm_d);
stopTime(&timer);
simTime2 = elapsedTime(timer);
cout << "Took " << setprecision(6) << simTime2 << " seconds." << endl;
totalKernel +=simTime2;
// Compute recommendations in parallel (kernel)
cout << "Similarity Kernel Result Verification: "
<< (verifySimilarityMatrix(similarityMatrix, similarityMatrixKernel) ? "SUCCESS" : "FAILURE") << endl;
cout << endl << "Calculating Top-" << n << " Recommendations for " << inputUserIds.size() << " users - Parallel." << endl;
vector<vector<ItemRating>> userRecommendationsKernel;
startTime(&timer);
for (unsigned int inputUserId : inputUserIds) {
vector<ItemRating> recommendations = calculateTopNRecommendationsForUserParallel(csrRowPtr_d, csrColIdx_d, csrData_d,
similarityMatrixKernel, movieIds, *ratingMatrix, inputUserId - 1, n);
userRecommendationsKernel.push_back(recommendations);
}
stopTime(&timer);
recTime2 = elapsedTime(timer);
for (unsigned int i = 0; i < inputUserIds.size(); i++)
storeRecommendationsToFile(userRecommendationsKernel[i], movieIdNameMapping,
"kernelRecommendations_" + to_string(inputUserIds[i]) + ".csv");
cout << "Took " << setprecision(6) << recTime2 << " seconds for " << inputUserIds.size() << " users." << endl;
totalKernel += recTime2;
printf("\nTotal Kernel Time: %f\n", totalKernel);
printf("Similarity Speedup: %f\n",(simTime/simTime2));
printf("Recommendation Speedup: %f\n",(recTime/recTime2));
printf("Total Speedup: %f\n",(totalGold/totalKernel));
cout << "Recommendations Kernel Result Verification: "
<< (verifyRecommendations(userRecommendations, userRecommendationsKernel) ? "SUCCESS" : "FAILURE") << endl;
// free all memories
freeDevicePtrs(csrRowPtr_d, csrColIdx_d, csrData_d, userEuclideanNorm_d);
free(similarityMatrix.similarities);
free(similarityMatrixKernel.similarities);
free(ratingMatrix);
}