Splitting the workload #10

Closed
opened 2025-01-26 00:29:12 +01:00 by sacha · 4 comments
Owner

It would be nice to be able to split work across multiple machines. Something like:

  • Machine 1: ./sudoku-funpark -workload 2 -part 1 ...etc...
  • Machine 2: ./sudoku-funpark -workload 2 -part 2 ...etc...

Goal is to specify how many parts there are, and which part the tool should take.

The hard part is figuring out how to evenly split the workload amongst x amount of machines. If the first row is completely filled, or has only one spot, and need to split the workload in two, it gets iffy. Same goes for first row being filled, and then working with 3 places in the second row.

I need to write an algorithm for that.

It would be nice to be able to split work across multiple machines. Something like: * Machine 1: `./sudoku-funpark -workload 2 -part 1 ...etc...` * Machine 2: `./sudoku-funpark -workload 2 -part 2 ...etc...` Goal is to specify how many parts there are, and which part the tool should take. The hard part is figuring out how to evenly split the workload amongst x amount of machines. If the first row is completely filled, or has only one spot, and need to split the workload in two, it gets iffy. Same goes for first row being filled, and then working with 3 places in the second row. I need to write an algorithm for that.
Author
Owner

First iteration would be just dealing with the first row.

  1. Check if the first row can be split up. If not, exit.
  2. Check if the first row can be divided by the workload:
  • If it can be divided, do it.
  • If not, have the first part have one extra, divide up the rest.

🤔 Maybe give it a thonk.

First iteration would be just dealing with the first row. 1. Check if the first row can be split up. If not, exit. 2. Check if the first row can be divided by the workload: - If it can be divided, do it. - If not, have the first part have one extra, divide up the rest. 🤔 Maybe give it a thonk.
Author
Owner

https://go.dev/play/p/GqX8a_vdkNd

package main

import (
	"fmt"
	"math"
	"strconv"
)

func main() {
	var tasks int = 159203
	var split int = 6
	var even_split float64

	even_split = float64(tasks) / float64(split)
	var mod_split int = tasks % split

	fmt.Println("tasks:" + strconv.Itoa(tasks))
	fmt.Println("split:" + strconv.Itoa(split))
	fmt.Println(even_split)
	fmt.Println(mod_split)
	fmt.Println("---")

	var sum int
	for part := range split {
		fmt.Println(part)
		if part == 0 {
			fmt.Println(math.Ceil(even_split))
			sum += int(math.Ceil(even_split))
		} else {
			fmt.Println(math.Floor(even_split))
			sum += int(math.Floor(even_split))
		}
	}
	fmt.Println("sum:" + strconv.Itoa(sum))
	fmt.Println("diff:" + strconv.Itoa(tasks-sum))
}

Did a proof of concept. I think I am better off creating an array with the size of split, and iterate through them, +1 each one till the diff is 0.

https://go.dev/play/p/GqX8a_vdkNd ```Go package main import ( "fmt" "math" "strconv" ) func main() { var tasks int = 159203 var split int = 6 var even_split float64 even_split = float64(tasks) / float64(split) var mod_split int = tasks % split fmt.Println("tasks:" + strconv.Itoa(tasks)) fmt.Println("split:" + strconv.Itoa(split)) fmt.Println(even_split) fmt.Println(mod_split) fmt.Println("---") var sum int for part := range split { fmt.Println(part) if part == 0 { fmt.Println(math.Ceil(even_split)) sum += int(math.Ceil(even_split)) } else { fmt.Println(math.Floor(even_split)) sum += int(math.Floor(even_split)) } } fmt.Println("sum:" + strconv.Itoa(sum)) fmt.Println("diff:" + strconv.Itoa(tasks-sum)) } ``` Did a proof of concept. I think I am better off creating an array with the size of `split`, and iterate through them, +1 each one till the diff is `0`.
Author
Owner

This worked like a charm.

package main

import "fmt"

func main() {
	var tasks int = 159203
	var split int = 6
	var tracker int

	agents := make([]int, split)

	for tasks != 0 {
		agents[tracker] += 1
		tasks -= 1
		tracker += 1
		if tracker == split {
			tracker = 0
		}
	}

	fmt.Println(agents)

}
This worked like a charm. ```Go package main import "fmt" func main() { var tasks int = 159203 var split int = 6 var tracker int agents := make([]int, split) for tasks != 0 { agents[tracker] += 1 tasks -= 1 tracker += 1 if tracker == split { tracker = 0 } } fmt.Println(agents) } ```
Author
Owner

Done with 74f1fb63e1
This was a fun one.

Done with https://gitea.ligthert.net/golang/sudoku-funpark/commit/74f1fb63e11d7030de1013a634435ed4ee11d056 This was a fun one.
sacha closed this issue 2025-01-27 00:13:35 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: golang/sudoku-funpark#10
No description provided.