Skip to content

Commit

Permalink
#74 Define go -> cgo -> cuda interface
Browse files Browse the repository at this point in the history
  • Loading branch information
hleb-albau authored and arturalbov committed Nov 16, 2018
1 parent 17c6b9d commit 65a2b6b
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ node_modules/
initial-rank
initial_guess_impact_results
*enwiki*
*.so
*.o
14 changes: 14 additions & 0 deletions cosmos/poc/cuda/README.md
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
```
17 changes: 17 additions & 0 deletions cosmos/poc/cuda/rank.cu
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]);
}
}
};
8 changes: 8 additions & 0 deletions cosmos/poc/cuda/rank.h
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 */
);
61 changes: 61 additions & 0 deletions cosmos/poc/cuda/test_type_compatibilities.go
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,
)
}
22 changes: 22 additions & 0 deletions cosmos/poc/cuda/types.h
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

0 comments on commit 65a2b6b

Please sign in to comment.