<commit_before>package main

import (
	"fmt"
	"github.com/codegangsta/cli"
	"image/png"
	"io/ioutil"
	"log"
	"os"
	"strings"
	"sync"
	"time"

	"github.com/DexterLB/traytor"
	"github.com/DexterLB/traytor/rpc"
	"github.com/valyala/gorpc"
)

type SampleCounter struct {
	sync.Mutex
	Counter int
}

func NewSampleCounter(value int) *SampleCounter {
	return &SampleCounter{Counter: value}
}

func (sc *SampleCounter) Dec() bool {
	sc.Lock()
	defer sc.Unlock()

	if sc.Counter > 0 {
		sc.Counter--
		return true
	}
	return false
}

func RenderLoop(
	sampleCounter *SampleCounter,
	client *gorpc.DispatcherClient,
	renderedImages chan<- *traytor.Image,
	width int,
	height int,
) {
	for {
		if !sampleCounter.Dec() {
			return
		}
		image, err := client.CallTimeout("Sample", [2]int{width, height}, 10*time.Minute)
		if err == nil {
			renderedImages <- image.(*traytor.Image)
			log.Printf("Rendered sample :)\n")
		} else {
			log.Printf("No sample :( %s\n", err)
		}
	}
}

func JoinSamples(
	renderedImages <-chan *traytor.Image,
	width int,
	height int,
) *traytor.Image {
	averageImage := traytor.NewImage(width, height)
	averageImage.Divisor = 0
	for image := range renderedImages {
		averageImage.Add(image)
		averageImage.Divisor += image.Divisor
	}
	return averageImage
}

func runClient(c *cli.Context) error {
	scene, image := getArguments(c)
	workers := c.StringSlice("worker")
	if len(workers) == 0 {
		showError(c, "can't render on zero workers :(")
	}

	fmt.Printf(
		"will render %s to %s of size %dx%d with %d threads on those workers: %s\n",
		scene, image,
		c.Int("width"), c.Int("height"),
		c.GlobalInt("max-jobs"),
		strings.Join(workers, ", "),
	)
	rr := rpc.NewRemoteRaytracer(time.Now().Unix())

	width, height := c.Int("width"), c.Int("height")
	sampleCounter := NewSampleCounter(30)
	renderedImages := make(chan *traytor.Image, 30)
	clients := make([]*gorpc.Client, len(workers))<commit_msg>Try remote render with 4 workers<commit_after>package main

import (
	"fmt"
	"github.com/codegangsta/cli"
	"image/png"
	"io/ioutil"
	"log"
	"os"
	"strings"
	"sync"
	"time"

	"github.com/DexterLB/traytor"
	"github.com/DexterLB/traytor/rpc"
	"github.com/valyala/gorpc"
)

type SampleCounter struct {
	sync.Mutex
	Counter int
}

func NewSampleCounter(value int) *SampleCounter {
	return &SampleCounter{Counter: value}
}

func (sc *SampleCounter) Dec() bool {
	sc.Lock()
	defer sc.Unlock()

	if sc.Counter > 0 {
		sc.Counter--
		return true
	}
	return false
}

func RenderLoop(
	sampleCounter *SampleCounter,
	client *gorpc.DispatcherClient,
	renderedImages chan<- *traytor.Image,
	width int,
	height int,
) {
	for {
		if !sampleCounter.Dec() {
			return
		}
		image, err := client.CallTimeout("Sample", [2]int{width, height}, 10*time.Minute)
		if err == nil {
			renderedImages <- image.(*traytor.Image)
			log.Printf("Rendered sample :)\n")
		} else {
			log.Printf("No sample :( %s\n", err)
		}
	}
}

func JoinSamples(
	renderedImages <-chan *traytor.Image,
	width int,
	height int,
) *traytor.Image {
	averageImage := traytor.NewImage(width, height)
	averageImage.Divisor = 0
	for image := range renderedImages {
		averageImage.Add(image)
		averageImage.Divisor += image.Divisor
	}
	return averageImage
}

func runClient(c *cli.Context) error {
	scene, image := getArguments(c)
	workers := c.StringSlice("worker")
	if len(workers) == 0 {
		showError(c, "can't render on zero workers :(")
	}

	fmt.Printf(
		"will render %s to %s of size %dx%d with %d threads on those workers: %s\n",
		scene, image,
		c.Int("width"), c.Int("height"),
		c.GlobalInt("max-jobs"),
		strings.Join(workers, ", "),
	)
	rr := rpc.NewRemoteRaytracer(time.Now().Unix())

	width, height := c.Int("width"), c.Int("height")
	sampleCounter := NewSampleCounter(50)
	renderedImages := make(chan *traytor.Image, 50)
	clients := make([]*gorpc.Client, len(workers))
{'fraction_non_alphanumeric': 0.15664845173041894, 'fraction_numerical': 0.00546448087431694, 'mean_word_length': 4.180592991913747, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2446242', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1445, 'n_words': 322}