Last active
June 14, 2020 00:40
-
-
Save selfup/918bc2875c60744e25b45426aa9e0bcb to your computer and use it in GitHub Desktop.
Burn in new CPU/RAM (otherwise completely useless) | Example: go run main.go -s 128 -w 4
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
package main | |
import ( | |
"flag" | |
"fmt" | |
"math/rand" | |
"os" | |
"sync" | |
"time" | |
) | |
const ( | |
min = 1 | |
max = 118 | |
) | |
// Atom atoms | |
type Atom struct { | |
x int | |
y int | |
z int | |
electrons int | |
protons int | |
neutrons int | |
} | |
// Payload payloads | |
type Payload struct { | |
X int | |
Y int | |
Z int | |
} | |
func main() { | |
var size int | |
flag.IntVar(&size, "s", 0, "size to be cubed") | |
var workers int | |
flag.IntVar(&workers, "w", 0, "workers to create atoms") | |
flag.Parse() | |
if size*workers == 0 { | |
flag.PrintDefaults() | |
os.Exit(2) | |
} | |
threads := make(map[int][]Payload) | |
rand.Seed(time.Now().UnixNano()) | |
count := 1 | |
for x := 0; x < size; x++ { | |
for y := 0; y < size; y++ { | |
for z := 0; z < size; z++ { | |
if count <= workers { | |
payload := Payload{x, y, z} | |
threads[count] = append(threads[count], payload) | |
count++ | |
} else { | |
count = 1 | |
} | |
} | |
} | |
} | |
var mtx sync.Mutex | |
var wg sync.WaitGroup | |
wg.Add(len(threads)) | |
var grid []Atom | |
for _, payloads := range threads { | |
payloads := payloads | |
go func() { | |
localGrid := createLocalGridSlice(payloads) | |
mtx.Lock() | |
grid = append(grid, localGrid...) | |
mtx.Unlock() | |
wg.Done() | |
}() | |
} | |
wg.Wait() | |
fmt.Println("grid size:", len(grid)) | |
} | |
func createLocalGridSlice(payloads []Payload) []Atom { | |
var localGrid []Atom | |
for _, payload := range payloads { | |
electrons := genRand() | |
protons := genRand() | |
neutrons := genRand() | |
x := payload.X | |
y := payload.Y | |
z := payload.Z | |
atom := Atom{x, y, z, electrons, protons, neutrons} | |
localGrid = append(localGrid, atom) | |
} | |
return localGrid | |
} | |
func genRand() int { | |
return rand.Intn(max-min) + min | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment