-
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.
- Loading branch information
1 parent
996b320
commit d44e0f0
Showing
7 changed files
with
191 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,18 @@ | ||
package rank | ||
|
||
import ( | ||
. "github.com/cybercongress/cyberd/cosmos/poc/app/storage" | ||
"sync" | ||
) | ||
import . "github.com/cybercongress/cyberd/cosmos/poc/app/storage" | ||
|
||
type ComputeUnit int | ||
|
||
const ( | ||
d float64 = 0.85 | ||
tolerance float64 = 1e-3 | ||
CPU ComputeUnit = iota | ||
GPU ComputeUnit = iota | ||
) | ||
|
||
func CalculateRank(data *InMemoryStorage) ([]float64, int) { | ||
|
||
inLinks := data.GetInLinks() | ||
|
||
size := data.GetCidsCount() | ||
if size == 0 { | ||
return []float64{}, 0 | ||
} | ||
|
||
rank := make([]float64, size) | ||
defaultRank := (1.0 - d) / float64(size) | ||
danglingNodesSize := uint64(0) | ||
|
||
for i := range rank { | ||
rank[i] = defaultRank | ||
if len(inLinks[CidNumber(i)]) == 0 { | ||
danglingNodesSize++ | ||
} | ||
} | ||
|
||
innerProductOverSize := defaultRank * (float64(danglingNodesSize) / float64(size)) | ||
defaultRankWithCorrection := float64(d*innerProductOverSize) + defaultRank | ||
|
||
change := tolerance + 1 | ||
|
||
steps := 0 | ||
prevrank := make([]float64, 0) | ||
prevrank = append(prevrank, rank...) | ||
for change > tolerance { | ||
rank = step(defaultRankWithCorrection, prevrank, data) | ||
change = calculateChange(prevrank, rank) | ||
prevrank = rank | ||
steps++ | ||
} | ||
|
||
return rank, steps | ||
} | ||
|
||
func step(defaultRankWithCorrection float64, prevrank []float64, data *InMemoryStorage) []float64 { | ||
|
||
rank := append(make([]float64, 0, len(prevrank)), prevrank...) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(len(data.GetInLinks())) | ||
|
||
for i, inLinksForI := range data.GetInLinks() { | ||
|
||
go func(cid CidNumber, inLinks CidLinks) { | ||
defer wg.Done() | ||
ksum := float64(0) | ||
|
||
//todo dependent on range iterator order, that non-deterministic | ||
for j := range inLinks { | ||
linkStake := data.GetOverallLinkStake(CidNumber(j), CidNumber(cid)) | ||
jCidOutStake := data.GetOverallOutLinksStake(CidNumber(j)) | ||
weight := float64(linkStake) / float64(jCidOutStake) | ||
ksum = float64(prevrank[j]*weight) + ksum //force no-fma here by explicit conversion | ||
} | ||
|
||
rank[cid] = float64(ksum*d) + defaultRankWithCorrection //force no-fma here by explicit conversion | ||
}(i, inLinksForI) | ||
} | ||
wg.Wait() | ||
return rank | ||
} | ||
|
||
func calculateChange(prevrank, rank []float64) float64 { | ||
|
||
maxDiff := 0.0 | ||
diff := 0.0 | ||
for i, pForI := range prevrank { | ||
if pForI > rank[i] { | ||
diff = pForI - rank[i] | ||
} else { | ||
diff = rank[i] - pForI | ||
} | ||
if diff > maxDiff { | ||
maxDiff = diff | ||
} | ||
func CalculateRank(data *InMemoryStorage, unit ComputeUnit) ([]float64, int) { | ||
if unit == CPU { | ||
return calculateRankCPU(data) | ||
} else { | ||
return calculateRankGPU(data) | ||
} | ||
|
||
return maxDiff | ||
} |
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,95 @@ | ||
package rank | ||
|
||
import ( | ||
. "github.com/cybercongress/cyberd/cosmos/poc/app/storage" | ||
"sync" | ||
) | ||
|
||
const ( | ||
d float64 = 0.85 | ||
tolerance float64 = 1e-3 | ||
) | ||
|
||
func calculateRankCPU(data *InMemoryStorage) ([]float64, int) { | ||
|
||
inLinks := data.GetInLinks() | ||
|
||
size := data.GetCidsCount() | ||
if size == 0 { | ||
return []float64{}, 0 | ||
} | ||
|
||
rank := make([]float64, size) | ||
defaultRank := (1.0 - d) / float64(size) | ||
danglingNodesSize := uint64(0) | ||
|
||
for i := range rank { | ||
rank[i] = defaultRank | ||
if len(inLinks[CidNumber(i)]) == 0 { | ||
danglingNodesSize++ | ||
} | ||
} | ||
|
||
innerProductOverSize := defaultRank * (float64(danglingNodesSize) / float64(size)) | ||
defaultRankWithCorrection := float64(d*innerProductOverSize) + defaultRank | ||
|
||
change := tolerance + 1 | ||
|
||
steps := 0 | ||
prevrank := make([]float64, 0) | ||
prevrank = append(prevrank, rank...) | ||
for change > tolerance { | ||
rank = step(defaultRankWithCorrection, prevrank, data) | ||
change = calculateChange(prevrank, rank) | ||
prevrank = rank | ||
steps++ | ||
} | ||
|
||
return rank, steps | ||
} | ||
|
||
func step(defaultRankWithCorrection float64, prevrank []float64, data *InMemoryStorage) []float64 { | ||
|
||
rank := append(make([]float64, 0, len(prevrank)), prevrank...) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(len(data.GetInLinks())) | ||
|
||
for i, inLinksForI := range data.GetInLinks() { | ||
|
||
go func(cid CidNumber, inLinks CidLinks) { | ||
defer wg.Done() | ||
ksum := float64(0) | ||
|
||
//todo dependent on range iterator order, that non-deterministic | ||
for j := range inLinks { | ||
linkStake := data.GetOverallLinkStake(CidNumber(j), CidNumber(cid)) | ||
jCidOutStake := data.GetOverallOutLinksStake(CidNumber(j)) | ||
weight := float64(linkStake) / float64(jCidOutStake) | ||
ksum = float64(prevrank[j]*weight) + ksum //force no-fma here by explicit conversion | ||
} | ||
|
||
rank[cid] = float64(ksum*d) + defaultRankWithCorrection //force no-fma here by explicit conversion | ||
}(i, inLinksForI) | ||
} | ||
wg.Wait() | ||
return rank | ||
} | ||
|
||
func calculateChange(prevrank, rank []float64) float64 { | ||
|
||
maxDiff := 0.0 | ||
diff := 0.0 | ||
for i, pForI := range prevrank { | ||
if pForI > rank[i] { | ||
diff = pForI - rank[i] | ||
} else { | ||
diff = rank[i] - pForI | ||
} | ||
if diff > maxDiff { | ||
maxDiff = diff | ||
} | ||
} | ||
|
||
return maxDiff | ||
} |
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,58 @@ | ||
package rank | ||
|
||
import ( | ||
"fmt" | ||
. "github.com/cybercongress/cyberd/cosmos/poc/app/storage" | ||
) | ||
|
||
/* | ||
#cgo CFLAGS: -I.3 | ||
#cgo LDFLAGS: -L. -lrank | ||
#cgo LDFLAGS: -lcudart | ||
#include "rank.h" | ||
*/ | ||
import "C" | ||
|
||
func calculateRankGPU(data *InMemoryStorage) ([]float64, int) { | ||
stakes := []uint64{3, 1, 2} | ||
|
||
inLinksCount := []uint32{0, 0, 1, 5, 4, 0, 1, 0} | ||
inLinksStartIndex := []uint64{0, 0, 0, 1, 6, 10, 10, 11} | ||
outLinksCount := []uint32{2, 2, 1, 1, 3, 1, 0, 1} | ||
outLinksStartIndex := []uint64{0, 2, 4, 5, 6, 9, 10, 10} | ||
|
||
inLinksOuts := []uint64{7, 1, 4, 4, 4, 2, 5, 0, 0, 1, 3} | ||
inLinksUsers := []uint64{0, 2, 0, 1, 2, 0, 1, 1, 2, 1, 1} | ||
outLinksUsers := []uint64{1, 2, 1, 2, 0, 1, 0, 1, 2, 1, 0} | ||
|
||
/* --- Convert to C ------------------------------- */ | ||
cStakesSize := C.ulong(len(stakes)) | ||
cCidsSize := C.ulong(len(inLinksStartIndex)) | ||
cLinksSize := C.ulong(len(inLinksOuts)) | ||
|
||
cStakes := (*C.ulong)(&stakes[0]) | ||
|
||
cInLinksStartIndex := (*C.ulong)(&inLinksStartIndex[0]) | ||
cInLinksCount := (*C.uint)(&inLinksCount[0]) | ||
|
||
cOutLinksStartIndex := (*C.ulong)(&outLinksStartIndex[0]) | ||
cOutLinksCount := (*C.uint)(&outLinksCount[0]) | ||
|
||
cInLinksOuts := (*C.ulong)(&inLinksOuts[0]) | ||
cInLinksUsers := (*C.ulong)(&inLinksUsers[0]) | ||
cOutLinksUsers := (*C.ulong)(&outLinksUsers[0]) | ||
|
||
/* --- Init rank ---------------------------------- */ | ||
rank := make([]float64, len(inLinksStartIndex)) | ||
cRank := (*C.double)(&rank[0]) | ||
/* --- Run Computation ---------------------------- */ | ||
fmt.Printf("Invoking cuda library...\n") | ||
C.calculate_rank( | ||
cStakes, cStakesSize, cCidsSize, cLinksSize, | ||
cInLinksStartIndex, cInLinksCount, | ||
cOutLinksStartIndex, cOutLinksCount, | ||
cInLinksOuts, cInLinksUsers, cOutLinksUsers, | ||
cRank, | ||
) | ||
return rank, 0 | ||
} |
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,11 @@ | ||
#include <stdint.h> | ||
|
||
void calculate_rank( | ||
uint64_t *stakes, uint64_t stakesSize, /* User stakes and corresponding array size */ | ||
uint64_t cidsSize, uint64_t linksSize, /* Cids count */ | ||
uint64_t *inLinksStartIndex, uint32_t *inLinksCount, /* array index - cid index*/ | ||
uint64_t *outLinksStartIndex, uint32_t *outLinksCount, /* array index - cid index*/ | ||
uint64_t *inLinksOuts, uint64_t *inLinksUsers, /*all incoming links from all users*/ | ||
uint64_t *outLinksUsers, /*all outgoing links from all users*/ | ||
double *rank /* array index - cid index*/ | ||
); |
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