aboutsummaryrefslogtreecommitdiff
path: root/fastnn/lib/ModelSync.c
blob: bd511ea57d769911be65adbb9cdc0138ae9c3f2e (plain) (blame)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "ModelSync.h"
#include "../../nerv/lib/matrix/cuda_helper.h"
#include "../../nerv/lib/matrix/generic/elem_type.h"
#include "common.h"
#include <string.h>


ModelSync* ModelSync_new(void)
{
	ModelSync *self = (ModelSync*)malloc(sizeof(ModelSync));
	if (NULL != self)
	{
		self->model_mutex = THMutex_new();
		self->state_mutex = THMutex_new();
		self->initialized_ = false;
		self->dim_ = 0;
		self->pos_ = 0;
		self->data_ = NULL;
		self->free_data_ = NULL;
		self->data_ = NULL;
		self->refcount = 1;
		self->threadcount = 0;
	}
	return self;
}

ModelSync* ModelSync_newWithId(long id)
{
	ModelSync *self = (ModelSync*)id;
	__sync_fetch_and_add(&self->refcount, 1);
	return self;
}

long ModelSync_id(ModelSync *self) 
{
	return (long)(self);
}

int  ModelSync_lockmodel(ModelSync *self)
{
	if(THMutex_lock(self->model_mutex))
		return 1;
	return 0;
}
	
int  ModelSync_unlockmodel(ModelSync *self)
{
	if(THMutex_unlock(self->model_mutex))
		return 1;
        return 0;

}
int  ModelSync_lockstate(ModelSync *self)
{
	if(THMutex_lock(self->state_mutex))
		return 1;
        return 0;
}

int  ModelSync_unlockstate(ModelSync *self)
{
	if(THMutex_unlock(self->state_mutex))
		return 1;
        return 0;
}

int  ModelSync_free(ModelSync *self)
{
	if (NULL != self && __sync_fetch_and_add(&self->refcount, -1) == 1)
	{
		free(self->model_mutex);	
		free(self->state_mutex);
		Status status;
		CUDA_SAFE_SYNC_CALL(cudaFreeHost(self->free_data_), &status);
		free(self);
	}
}

int  ModelSync_initBuffer(ModelSync *self)
{
	if (NULL != self)
	{
		void *free_data = NULL, *data = NULL;
		size_t size = self->dim_ * sizeof(float)+16;
		Status status;
		CUDA_SAFE_SYNC_CALL(cudaHostAlloc((void**) &free_data, size, cudaHostAllocPortable), &status);	
		NERV_SET_STATUS(&status, NERV_NORMAL, 0);

		data = (free_data ? (void *)( (((unsigned long)*(&free_data)) + 15) & ~0xFUL ) : NULL) ;
		if (NULL != data)
		{
			self->data_ =  (float*)(data);
			self->free_data_ = (float*)(free_data);
		}
		return 0;
	}
	return 1;
}

int  ModelSync_weightfromd(ModelSync *self, Matrix *dm)
{

	if (NULL != self && NULL != dm)
	{
		void *host_data_ = (void*)self->data_;
		size_t width = dm->ncol * sizeof(float);
		size_t src_pitch = dm->stride;
		size_t dst_pitch = src_pitch;	
		Status status;

		CUDA_SAFE_SYNC_CALL(cudaMemcpy2D(host_data_+self->pos_, dst_pitch, dm->data.f, src_pitch, width, dm->nrow, cudaMemcpyDeviceToHost), &status);
		NERV_SET_STATUS(&status, NERV_NORMAL, 0);
		self->pos_ += dm->nrow * dm->stride;
		return 0;
	}
	return 1;
	
}


int  ModelSync_weighttod(ModelSync *self, Matrix *dm)
{

        if (NULL != self && NULL != dm) 
        {   
		void *host_data_ = (void*)self->data_;
                size_t width = dm->ncol * sizeof(float);
                size_t dst_pitch = dm->stride;
                size_t src_pitch = dst_pitch;   
		Status status;

                CUDA_SAFE_SYNC_CALL(cudaMemcpy2D(dm->data.f, dst_pitch, host_data_+self->pos_, src_pitch, width, dm->nrow, cudaMemcpyHostToDevice), &status);
		NERV_SET_STATUS(&status, NERV_NORMAL, 0);

                self->pos_ += dm->nrow * dm->stride;
		self->initialized_ = true;
		return 0;
        }   
	return 1;
}

void ModelSync_syncinc(ModelSync *self)
{
	__sync_fetch_and_add(&self->threadcount, 1);
}

void ModelSync_syncdec(ModelSync *self)
{
	__sync_fetch_and_add(&self->threadcount, -1);
}

int ModelSync_threadcount(ModelSync *self)
{
	return self->threadcount;
}

/////////////////////////////////

Xent* Xent_new()
{
	Xent *xent = (Xent*)malloc(sizeof(Xent));
	memset(xent, 0, sizeof(Xent));
	xent->refcount = 1;
	return xent;
}

Xent* Xent_newWithId(long id)
{
	Xent *xent = (Xent*)id;
	__sync_fetch_and_add(&xent->refcount, 1);
	return xent;
}

Xent* Xent_newWithParm(size_t frames_, size_t correct_, double loss_, double entropy_)
{
	Xent *xent = (Xent*)malloc(sizeof(Xent));
	xent->frames_ = frames_;
	xent->correct_ = correct_;
	xent->loss_ = loss_;
	xent->entropy_ = entropy_;
	xent->refcount = 1;
	return xent;
}

long Xent_id(Xent *xent)
{
	return (long)(xent);
}

Xent* Xent_add(Xent *a, Xent *b)
{
	a->frames_ += b->frames_;
	a->correct_ += b->correct_;
	a->loss_ += b->loss_;
	a->entropy_ += b->entropy_;
	return a;
}

void Xent_free(Xent *xent)
{	
	if (NULL != xent && __sync_fetch_and_add(&xent->refcount, -1) == 1)
	{ 
		free(xent);
		xent = NULL;
	}
}


//////////////////////////////////

Mse* Mse_new()
{
        Mse *mse = (Mse*)malloc(sizeof(Mse));
        memset(mse, 0, sizeof(Mse));
	mse->refcount = 1;
        return mse;
}

Mse* Mse_newWithId(long id)
{
        Mse *mse = (Mse*)id;
        __sync_fetch_and_add(&mse->refcount, 1);
        return mse;
}

Mse* Mse_newWithParm(size_t frames_, double loss_)
{
        Mse *mse = (Mse*)malloc(sizeof(Mse));
        mse->frames_ = frames_;
        mse->loss_ = loss_;
	mse->refcount = 1;
        return mse;
}


long Mse_id(Mse *mse)
{
        return (long)(mse);
}

Mse* Mse_add(Mse *a, Mse *b)
{
        a->frames_ += b->frames_;
        a->loss_ += b->loss_;
        return a;
}

void Mse_free(Mse *mse)
{
        if (NULL != mse && __sync_fetch_and_add(&mse->refcount, -1) == 1)
        {
                free(mse);
                mse = NULL;
        }
}

//////////////////////////////////

GlobalOption* GlobalOption_new()
{
        GlobalOption *option = (GlobalOption*)malloc(sizeof(GlobalOption));
        option->refcount = 1;
        return option;
}

GlobalOption* GlobalOption_newWithParm(int batch_size, float lrate, bool bp,const char *tr_scp, const char *cv_scp, const char *transf, const char *network)
{
        GlobalOption *option = (GlobalOption*)malloc(sizeof(GlobalOption));
	option->batch_size = batch_size;
	option->lrate = lrate;
	option->bp = bp;
	strncpy(option->tr_scp, tr_scp, strlen(tr_scp)+1);
	strncpy(option->cv_scp, cv_scp, strlen(cv_scp)+1);
	strncpy(option->transf, transf, strlen(transf)+1);
	strncpy(option->network, network, strlen(network)+1);
        option->refcount = 1;

        return option;
}

GlobalOption* GlobalOption_newWithId(long