-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2468 from helinwang/master_dispatch
Implement master client for reading training tasks
- Loading branch information
Showing
18 changed files
with
456 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
cmake_minimum_required(VERSION 3.0) | ||
|
||
get_filename_component(PARENT_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) | ||
get_filename_component(PARENT_DIR ${PARENT_DIR} DIRECTORY) | ||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PARENT_DIR}/cmake") | ||
|
||
project(cxx_go C Go) | ||
|
||
include(golang) | ||
include(flags) | ||
|
||
set(MASTER_LIB_NAME "paddle_master") | ||
go_library(${MASTER_LIB_NAME} SHARED) | ||
|
||
if(PROJ_ROOT) | ||
add_custom_command(OUTPUT ${PROJ_ROOT}/python/paddle/v2/master/lib${MASTER_LIB_NAME}.so | ||
COMMAND rm ${CMAKE_CURRENT_BINARY_DIR}/lib${MASTER_LIB_NAME}.h | ||
COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/lib${MASTER_LIB_NAME}.so ${PROJ_ROOT}/python/paddle/v2/master/ | ||
DEPENDS ${MASTER_LIB_NAME}) | ||
add_custom_target(paddle_master_shared ALL DEPENDS ${PROJ_ROOT}/python/paddle/v2/master/lib${MASTER_LIB_NAME}.so) | ||
endif(PROJ_ROOT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package main | ||
|
||
/* | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdio.h> | ||
#define PADDLE_MASTER_OK 0 | ||
#define PADDLE_MASTER_ERROR -1 | ||
typedef int paddle_master_client; | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"sync" | ||
"unsafe" | ||
|
||
"github.com/PaddlePaddle/Paddle/go/master" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var nullPtr = unsafe.Pointer(uintptr(0)) | ||
var mu sync.Mutex | ||
var handleMap = make(map[C.paddle_master_client]*master.Client) | ||
var curHandle C.paddle_master_client | ||
|
||
func add(c *master.Client) C.paddle_master_client { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
client := curHandle | ||
curHandle++ | ||
handleMap[client] = c | ||
return client | ||
} | ||
|
||
func get(client C.paddle_master_client) *master.Client { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
return handleMap[client] | ||
} | ||
|
||
func remove(client C.paddle_master_client) *master.Client { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
h := handleMap[client] | ||
delete(handleMap, client) | ||
return h | ||
} | ||
|
||
type addresser string | ||
|
||
func (a addresser) Address() string { | ||
return string(a) | ||
} | ||
|
||
//export paddle_new_master_client | ||
func paddle_new_master_client(addr *C.char, bufSize int) C.paddle_master_client { | ||
a := C.GoString(addr) | ||
c := master.NewClient(addresser(a), bufSize) | ||
return add(c) | ||
} | ||
|
||
//export paddle_release_master_client | ||
func paddle_release_master_client(client C.paddle_master_client) { | ||
remove(client) | ||
} | ||
|
||
//export paddle_set_dataset | ||
func paddle_set_dataset(client C.paddle_master_client, path **C.char, size C.int) C.int { | ||
c := get(client) | ||
var paths []string | ||
for i := 0; i < int(size); i++ { | ||
ptr := (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(path)) + uintptr(i)*unsafe.Sizeof(*path))) | ||
str := C.GoString(*ptr) | ||
paths = append(paths, str) | ||
} | ||
err := c.SetDataset(paths) | ||
if err != nil { | ||
log.Errorln(err) | ||
return C.PADDLE_MASTER_ERROR | ||
} | ||
|
||
return C.PADDLE_MASTER_OK | ||
} | ||
|
||
//export paddle_next_record | ||
func paddle_next_record(client C.paddle_master_client, record **C.uchar) C.int { | ||
c := get(client) | ||
r := c.NextRecord() | ||
if len(r) == 0 { | ||
*record = (*C.uchar)(nullPtr) | ||
return 0 | ||
} | ||
|
||
size := C.size_t(len(r)) | ||
*record = (*C.uchar)(C.malloc(size)) | ||
C.memcpy(unsafe.Pointer(*record), unsafe.Pointer(&r[0]), size) | ||
return C.int(size) | ||
} | ||
|
||
//export mem_free | ||
func mem_free(p unsafe.Pointer) { | ||
// "free" may be a better name for this function, but doing so | ||
// will cause calling any function of this library from Python | ||
// ctypes hanging. | ||
C.free(p) | ||
} | ||
|
||
func main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.