-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#74 Define go -> cgo -> cuda interface
- Loading branch information
1 parent
17c6b9d
commit 65a2b6b
Showing
6 changed files
with
124 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ node_modules/ | |
initial-rank | ||
initial_guess_impact_results | ||
*enwiki* | ||
*.so | ||
*.o |
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,14 @@ | ||
# Cuda support | ||
|
||
## Install required libs | ||
```bash | ||
|
||
``` | ||
|
||
## Development | ||
To execute cuda code run. | ||
|
||
```bash | ||
nvcc -shared -o librank.so rank.cu --compiler-options '-fPIC' && sudo cp librank.so /usr/lib/ | ||
go run *.go | ||
``` |
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,17 @@ | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include "types.h" | ||
|
||
extern "C" { | ||
|
||
void calculate_rank( | ||
uint64_t *stakes, uint64_t stakesSize, /* User stakes and corresponding array size */ | ||
cid *cids, uint64_t cidsSize, /* Cids links */ | ||
cid_link *inLinks, cid_link *outLinks /* Incoming and Outgoing cids links */ | ||
) { | ||
|
||
for(int i = 0; i < stakesSize; i++) { | ||
printf("%lu \n", stakes[i]); | ||
} | ||
} | ||
}; |
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,8 @@ | ||
#include <stdint.h> | ||
#include "types.h" | ||
|
||
void calculate_rank( | ||
uint64_t *stakes, uint64_t stakesSize, /* User stakes and corresponding array size */ | ||
cid **cids, uint64_t cidsSize, /* Cids links */ | ||
cid_link **inLinks, cid_link **outLinks /* Incoming and Outgoing cids links */ | ||
); |
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,61 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"unsafe" | ||
) | ||
|
||
/* | ||
#cgo CFLAGS: -I. | ||
#cgo LDFLAGS: -L. -lrank | ||
#cgo LDFLAGS: -lcudart | ||
#include "rank.h" | ||
*/ | ||
import "C" | ||
|
||
type CidLink struct { | ||
oppositeCidIndex uint64 | ||
userIndex uint64 | ||
} | ||
|
||
type Cid struct { | ||
inLinksStartIndex uint64 | ||
inLinksCount uint64 | ||
outLinksStartIndex uint64 | ||
outLinksCount uint64 | ||
} | ||
|
||
func main() { | ||
|
||
/* --- Stakes ------------------------------------ */ | ||
stakes := make([]uint64, 2) | ||
|
||
for i := range stakes { | ||
stakes[i] = uint64(rand.Intn(30)) | ||
} | ||
|
||
cStakes := (*C.ulong)(&stakes[0]) | ||
cStakesLen := C.ulong(len(stakes)) | ||
|
||
/* --- Cids ------------------------------------ */ | ||
cids := make([]Cid, 10) | ||
|
||
cCids := (**C.cid)(unsafe.Pointer(&cids[0])) | ||
cCidsLen := C.ulong(len(cids)) | ||
|
||
/* --- Cids Links ------------------------------- */ | ||
inLinks := make([]CidLink, 100) | ||
outLinks := make([]CidLink, 100) | ||
|
||
cInLinks := (**C.cid_link)(unsafe.Pointer(&inLinks[0])) | ||
cOutLinks := (**C.cid_link)(unsafe.Pointer(&outLinks[0])) | ||
|
||
/* --- Run Computation -------------------------- */ | ||
fmt.Printf("Invoking cuda library...\n") | ||
C.calculate_rank( | ||
cStakes, cStakesLen, | ||
cCids, cCidsLen, | ||
cInLinks, cOutLinks, | ||
) | ||
} |
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,22 @@ | ||
#ifndef TYPES_HEADER_FILE | ||
#define TYPES_HEADER_FILE | ||
|
||
#include <stdint.h> | ||
|
||
/* Go -> CGO passing types */ | ||
typedef struct { | ||
/* Index of opposite cid in cids array */ | ||
uint64_t opposite_cid_index; | ||
/* Index of user stake in stakes array */ | ||
uint64_t user_index; | ||
} cid_link; | ||
|
||
typedef struct { | ||
uint64_t in_links_start_index; | ||
uint64_t in_links_count; | ||
|
||
uint64_t out_links_start_index; | ||
uint64_t out_links_count; | ||
} cid; | ||
|
||
#endif |