-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Implement master client for reading training tasks #2468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fa5c3f1
implement master client, Go part
helinwang 7b9080e
Implement master client, cgo and Python part
helinwang 4970484
improve comment, fix build error
helinwang e730bc5
improve comment
helinwang 094106a
use logrus for logging
helinwang 6cd1441
add bufSize parameter for creating master client
helinwang 8742441
integrate master Python lib with cmake
helinwang 4b6243c
fix cmake format
helinwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line will block util some reader read the record from this channel. How to deal with "batches" like
paddle.dataset.batch(some_reader(), 32)
? Will adding channel buffers likec.ch = make(chan []byte, batch_size)
increase the efficiency?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use
paddle.reader.buffered
for this purpose. Still I think that's a good point. Will add.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.