aboutsummaryrefslogtreecommitdiff
path: root/nerv/lib/matrix/mmatrix.c
blob: f1cbc75016a946683049f4264b85063945139542 (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
#define NERV_GENERIC_MMATRIX
#define MATRIX_CONTEXT MContext
#include <stdlib.h>
#include "../common.h"
#include "mmatrix.h"

#define MATRIX_USE_FLOAT
#define host_matrix_(NAME) host_matrix_float_##NAME
#define nerv_matrix_(NAME) nerv_matrix_host_float_##NAME
#define NERV_CBLAS_(NAME) cblas_s##NAME
#include "generic/matrix.h"
#include "generic/elem_type.h"
#include "generic/mmatrix.c"

void nerv_host_context_print_profile(MContext *context) {
    HashMap *profile = context->profile;
    size_t i;
    fprintf(stderr, "*** [nerv mmatrix profile] **\n");
    for (i = 0; i < profile->size; i++)
    {
        HashNode *ptr;
        for (ptr = profile->bucket[i]; ptr; ptr = ptr->next)
        {
            fprintf(stderr, "%s:\t%.6f\n", ptr->key, *(float *)ptr->val);
        }
    }
}

void nerv_host_context_clear_profile(MContext *context) {
    nerv_hashmap_clear(context->profile);
}

MContext *nerv_host_context_create(Status *status) {
    MContext *context = (MContext *)malloc(sizeof(MContext));
    context->profile = nerv_hashmap_create(PROFILE_HASHMAP_SIZE, bkdr_hash, strcmp);
    NERV_SET_STATUS(status, NERV_NORMAL, 0);
    return context;
}

void nerv_host_context_destroy(MContext *context, Status *status) {
    nerv_hashmap_destroy(context->profile);
    free(context);
    NERV_SET_STATUS(status, NERV_NORMAL, 0);
}

Matrix *nerv_matrix_(perm_gen)(int ncol, MContext *context, Status *status) {
    int i;
    Matrix *self = nerv_matrix_(create)(1, ncol, context, status);
    if (status->err_code != NERV_NORMAL)
        return NULL;
    float *prow = MATRIX_ELEM_PTR_F(self);
    for (i = 0; i < ncol; i++)
        prow[i] = i;
    for (i = ncol - 1; i >= 0; i--)
    {
        size_t j = rand() % (i + 1);
        float tmp = prow[i];
        prow[i] = prow[j];
        prow[j] = tmp;
    }
    return self;
}

#undef nerv_matrix_
#undef host_matrix_
#undef NERV_CBLAS_
#undef MATRIX_USE_FLOAT
#undef MATRIX_ELEM
#undef MATRIX_ELEM_PTR
#undef MATRIX_ELEM_PTR_BASE
#undef MATRIX_ELEM_FMT
#undef MATRIX_ELEM_WRITE_FMT

#define NERV_GENERIC_MMATRIX
#define MATRIX_USE_DOUBLE
#define host_matrix_(NAME) host_matrix_double_##NAME
#define nerv_matrix_(NAME) nerv_matrix_host_double_##NAME
#define NERV_CBLAS_(NAME) cblas_d##NAME
#include "generic/mmatrix.c"
#undef nerv_matrix_
#undef host_matrix_
#undef NERV_CBLAS_
#undef MATRIX_USE_DOUBLE
#undef MATRIX_ELEM
#undef MATRIX_ELEM_PTR
#undef MATRIX_ELEM_PTR_BASE
#undef MATRIX_ELEM_FMT
#undef MATRIX_ELEM_WRITE_FMT

#define NERV_GENERIC_MMATRIX
#define MATRIX_USE_INT
#define host_matrix_(NAME) host_matrix_int_##NAME
#define nerv_matrix_(NAME) nerv_matrix_host_int_##NAME
#define NERV_CBLAS_(NAME) (void)
#include "generic/mmatrix.c"