Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

FINAL_CFLAGS=$(CFLAGS)
FINAL_CFLAGS=$(CFLAGS) -g -DREDISMODULE_EXPERIMENTAL_API
FINAL_LDFLAGS=$(LDFLAGS)
FINAL_LDPATH=
TS_CC=gcc
Expand All @@ -9,7 +9,7 @@ DEPS_PATH=../deps
INSTALL_PATH=../install/
INSTALL=install

TS_OBJ = redisdl.o
TS_OBJ = redisdl.o tensor.o graph.o

INCLUDE_FLAGS=-I $(DEPS_PATH)/redis/src -I $(DEPS_PATH)/libtensorflow/include
LDPATH_FLAGS=-L $(DEPS_PATH)/libtensorflow/lib
Expand Down
102 changes: 102 additions & 0 deletions src/graph.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "graph.h"

RedisModuleType *RedisDL_GraphType = NULL;

static void* Graph_RdbLoad(struct RedisModuleIO *io, int encver){
//todo
return NULL;
}

static void Graph_RdbSave(RedisModuleIO *rdb, void *value){
//todo
}

static void Graph_DTFree(void *value){
Graph_Free(value);
}

int Graph_Init(RedisModuleCtx* ctx){
RedisModuleTypeMethods tmGraph = {
.version = REDISMODULE_TYPE_METHOD_VERSION,
.rdb_load = Graph_RdbLoad,
.rdb_save = Graph_RdbSave,
.aof_rewrite = NULL,
.mem_usage = NULL,
.free = Graph_DTFree,
.digest = NULL
};

RedisDL_GraphType = RedisModule_CreateDataType(ctx, "DL__GRAPH", 0, &tmGraph);
return RedisDL_GraphType != NULL;
}

RDL_Graph* Graph_Create(const char* prefix, const char* graphdef, size_t graphlen){
TF_Graph* graph = TF_NewGraph();

TF_ImportGraphDefOptions* options = TF_NewImportGraphDefOptions();
TF_ImportGraphDefOptionsSetPrefix(options, prefix);

TF_Buffer *buffer = TF_NewBuffer();
buffer->length = graphlen;
buffer->data = graphdef;

TF_Status *status = TF_NewStatus();

TF_GraphImportGraphDef(graph, buffer, options, status);

if (TF_GetCode(status) != TF_OK) {
// todo: free memory
return NULL;
}

TF_DeleteImportGraphDefOptions(options);
TF_DeleteBuffer(buffer);
TF_DeleteStatus(status);

TF_Status *sessionStatus = TF_NewStatus();

TF_SessionOptions *sessionOptions = TF_NewSessionOptions();
TF_Session *session = TF_NewSession(graph, sessionOptions, sessionStatus);

if (TF_GetCode(sessionStatus) != TF_OK) {
// TODO: free memory
return NULL;
}

TF_DeleteSessionOptions(sessionOptions);
TF_DeleteStatus(sessionStatus);

RDL_Graph* ret = RedisModule_Alloc(sizeof(*ret));
ret->graph = graph;
ret->session = session;
ret->refCount = 1;

return ret;
}

void Graph_Free(RDL_Graph* graph){
TF_Status *status = TF_NewStatus();
TF_CloseSession(graph->session, status);

if (TF_GetCode(status) != TF_OK) {
// TODO: raise error but we don't have a hold on ctx (that's because the caller _Free_ doesn't)
// return RedisModule_ReplyWithError(ctx, TF_Message(status));
return;
}

TF_DeleteSession(graph->session, status);
graph->session = NULL;

if (TF_GetCode(status) != TF_OK) {
// TODO: raise error but we don't have a hold on ctx (that's because the caller _Free_ doesn't)
// return RedisModule_ReplyWithError(ctx, TF_Message(status));
return;
}

TF_DeleteGraph(graph->graph);
graph->graph = NULL;

TF_DeleteStatus(status);

RedisModule_Free(graph);
}
31 changes: 31 additions & 0 deletions src/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* graph.h
*
* Created on: 28 Nov 2018
* Author: root
*/

#ifndef SRC_GRAPH_H_
#define SRC_GRAPH_H_

#include "tensorflow/c/c_api.h"
#include "redismodule.h"

typedef struct RDL_Graph{
TF_Graph* graph;
// TODO: use session pool? The ideal would be to use one session per client.
// If a client disconnects, we dispose the session or reuse it for
// another client.
void *session;
size_t refCount;
}RDL_Graph;

extern RedisModuleType *RedisDL_GraphType;

int Graph_Init(RedisModuleCtx* ctx);
RDL_Graph* Graph_Create(const char* prefix, const char* graphdef, size_t graphlen);
void Graph_Free(RDL_Graph* graph);



#endif /* SRC_GRAPH_H_ */
Loading