forked from cuMF/cumf_als
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
201 lines (174 loc) · 6.4 KB
/
main.cpp
File metadata and controls
201 lines (174 loc) · 6.4 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* main.cpp
*
* Created on: Feb 10, 2015
* Author: Wei Tan (wtan@us.ibm.com)
* Test als.cu using netflix or yahoo data
* Alternating Least Square for Matrix Factorization on CUDA 7.0+
* Code optimized for F = 100, and on cc 3.5, 3.7 platforms. Also tested in cc 5.2
*/
#include "als.h"
#include "host_utilities.h"
#include<stdlib.h>
#include<stdio.h>
#include <string>
#define DEVICEID 0
#define ITERS 10
/*
//netflix standard data
#define M 17770
#define N 480189
#define NNZ 99072112
#define NNZ_TEST 1408395
#define X_BATCH 1
*/
//lambda: K40 and Maxwell: 0.055
//K80: needs 0.06
/*
//yahoo data
#define M 1000990
#define N 624961
#define NNZ 252800275
#define NNZ_TEST 4003960
//1.2 on K40, Maxwell
//need to be 2.0+ on K80
#define LAMBDA 1.1
#define THETA_BATCH 3
#define X_BATCH 6
*/
int main(int argc, char **argv) {
//parse input parameters
if(argc != 10){
printf("Usage: give M, N, F, NNZ, NNZ_TEST, lambda, X_BATCH, THETA_BATCH and DATA_DIR.\n");
printf("E.g., for netflix data set, use: \n");
printf("./main 17770 480189 100 99072112 1408395 0.058 1 3 ./data/netflix/ \n");
printf("E.g., for movielens 10M data set, use: \n");
printf("./main 71567 65133 100 9000048 1000006 0.05 1 1 ./data/ml10M/ \n");
printf("E.g., for yahooMusic data set, use: \n");
printf("./main 1000990 624961 100 252800275 4003960 1.1 6 3 ./data/yahoo/ \n");
return 0;
}
int f = atoi(argv[3]);
if(f%T10!=0){
printf("F has to be a multiple of %d \n", T10);
return 0;
}
int m = atoi(argv[1]);
int n = atoi(argv[2]);
long nnz = atoi(argv[4]);
long nnz_test = atoi(argv[5]);
float lambda = atof(argv[6]);
int X_BATCH = atoi(argv[7]);
int THETA_BATCH = atoi(argv[8]);
std::string DATA_DIR(argv[9]);
printf("M = %d, N = %d, F = %d, NNZ = %ld, NNZ_TEST = %ld, lambda = %f\nX_BATCH = %d, THETA_BATCH = %d\nDATA_DIR = %s \n",
m, n, f, nnz, nnz_test, lambda, X_BATCH, THETA_BATCH, DATA_DIR.c_str());
cudaSetDevice(DEVICEID);
int* csrRowIndexHostPtr;
cudacall(cudaMallocHost( (void** ) &csrRowIndexHostPtr, (m + 1) * sizeof(csrRowIndexHostPtr[0])) );
int* csrColIndexHostPtr;
cudacall(cudaMallocHost( (void** ) &csrColIndexHostPtr, nnz * sizeof(csrColIndexHostPtr[0])) );
float* csrValHostPtr;
cudacall(cudaMallocHost( (void** ) &csrValHostPtr, nnz * sizeof(csrValHostPtr[0])) );
float* cscValHostPtr;
cudacall(cudaMallocHost( (void** ) &cscValHostPtr, nnz * sizeof(cscValHostPtr[0])) );
int* cscRowIndexHostPtr;
cudacall(cudaMallocHost( (void** ) &cscRowIndexHostPtr, nnz * sizeof(cscRowIndexHostPtr[0])) );
int* cscColIndexHostPtr;
cudacall(cudaMallocHost( (void** ) &cscColIndexHostPtr, (n+1) * sizeof(cscColIndexHostPtr[0])) );
int* cooRowIndexHostPtr;
cudacall(cudaMallocHost( (void** ) &cooRowIndexHostPtr, nnz * sizeof(cooRowIndexHostPtr[0])) );
//calculate X from thetaT first, need to initialize thetaT
float* thetaTHost;
cudacall(cudaMallocHost( (void** ) &thetaTHost, n * f * sizeof(thetaTHost[0])) );
float* XTHost;
cudacall(cudaMallocHost( (void** ) &XTHost, m * f * sizeof(XTHost[0])) );
//initialize thetaT on host
unsigned int seed = 10;
srand (seed);
for (int k = 0; k < n * f; k++)
//netflix standard
thetaTHost[k] = 0.05*((float) rand() / (RAND_MAX)) - 0.35;
//yahoo
//thetaTHost[k] = 3.0*((float) rand() / (RAND_MAX)) - 1.0f;
//CG needs to initialize X as well
for (int k = 0; k < m * f; k++)
//netflix standard
XTHost[k] = 0.05*((float) rand() / (RAND_MAX)) - 0.35;
printf("*******start loading training and testing sets to host.\n");
//testing set
int* cooRowIndexTestHostPtr = (int *) malloc(
nnz_test * sizeof(cooRowIndexTestHostPtr[0]));
int* cooColIndexTestHostPtr = (int *) malloc(
nnz_test * sizeof(cooColIndexTestHostPtr[0]));
float* cooValHostTestPtr = (float *) malloc(nnz_test * sizeof(cooValHostTestPtr[0]));
struct timeval tv0;
gettimeofday(&tv0, NULL);
loadCooSparseMatrixBin( (DATA_DIR + "/R_test_coo.data.bin").c_str(), (DATA_DIR + "/R_test_coo.row.bin").c_str(),
(DATA_DIR + "/R_test_coo.col.bin").c_str(),
cooValHostTestPtr, cooRowIndexTestHostPtr, cooColIndexTestHostPtr, nnz_test);
loadCSRSparseMatrixBin( (DATA_DIR + "/R_train_csr.data.bin").c_str(), (DATA_DIR + "/R_train_csr.indptr.bin").c_str(),
(DATA_DIR + "/R_train_csr.indices.bin").c_str(),
csrValHostPtr, csrRowIndexHostPtr, csrColIndexHostPtr, m, nnz);
loadCSCSparseMatrixBin( (DATA_DIR + "/R_train_csc.data.bin").c_str(), (DATA_DIR + "/R_train_csc.indices.bin").c_str(),
(DATA_DIR +"/R_train_csc.indptr.bin").c_str(),
cscValHostPtr, cscRowIndexHostPtr, cscColIndexHostPtr, n, nnz);
loadCooSparseMatrixRowPtrBin( (DATA_DIR + "/R_train_coo.row.bin").c_str(), cooRowIndexHostPtr, nnz);
#ifdef DEBUG
printf("\nloaded training csr to host; print data, row and col array\n");
for (int i = 0; i < nnz && i < 10; i++) {
printf("%.1f ", csrValHostPtr[i]);
}
printf("\n");
for (int i = 0; i < nnz && i < 10; i++) {
printf("%d ", csrRowIndexHostPtr[i]);
}
printf("\n");
for (int i = 0; i < nnz && i < 10; i++) {
printf("%d ", csrColIndexHostPtr[i]);
}
printf("\n");
printf("\nloaded testing coo to host; print data, row and col array\n");
for (int i = 0; i < nnz && i < 10; i++) {
printf("%.1f ", cooValHostTestPtr[i]);
}
printf("\n");
for (int i = 0; i < nnz && i < 10; i++) {
printf("%d ", cooRowIndexTestHostPtr[i]);
}
printf("\n");
for (int i = 0; i < nnz && i < 10; i++) {
printf("%d ", cooColIndexTestHostPtr[i]);
}
printf("\n");
#endif
double t0 = seconds();
doALS(csrRowIndexHostPtr, csrColIndexHostPtr, csrValHostPtr,
cscRowIndexHostPtr, cscColIndexHostPtr, cscValHostPtr,
cooRowIndexHostPtr, thetaTHost, XTHost,
cooRowIndexTestHostPtr, cooColIndexTestHostPtr, cooValHostTestPtr,
m, n, f, nnz, nnz_test, lambda,
ITERS, X_BATCH, THETA_BATCH, DEVICEID);
printf("\ndoALS takes seconds: %.3f for F = %d\n", seconds() - t0, f);
/*
//write out the model
FILE * xfile = fopen("XT-CG.data", "wb");
FILE * thetafile = fopen("thetaT-CG.data", "wb");
fwrite(XTHost, sizeof(float), m*f, xfile);
fwrite(thetaTHost, sizeof(float), n*f, thetafile);
fclose(xfile);
fclose(thetafile);
*/
cudaFreeHost(csrRowIndexHostPtr);
cudaFreeHost(csrColIndexHostPtr);
cudaFreeHost(csrValHostPtr);
cudaFreeHost(cscValHostPtr);
cudaFreeHost(cscRowIndexHostPtr);
cudaFreeHost(cscColIndexHostPtr);
cudaFreeHost(cooRowIndexHostPtr);
cudaFreeHost(XTHost);
cudaFreeHost(thetaTHost);
cudacall(cudaDeviceReset());
printf("\nALS Done.\n");
return 0;
}