parallel

package module
v0.0.0-...-03a805e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2018 License: MIT Imports: 6 Imported by: 0

README

========
parallel
========

|image0|_ |image1|_

.. |image0| image:: https://godoc.org/github.com/eraclitux/parallel?status.svg
.. _image0: https://godoc.org/github.com/eraclitux/parallel

.. |image1| image:: https://travis-ci.org/eraclitux/parallel.svg?branch=master
.. _image1: https://travis-ci.org/eraclitux/parallel

Package ``parallel`` tries to simplify use of parallel (as not concurrent) workers that run on their own core.
Number of workers is adjusted at runtime in base of numbers of cores.
This paradigm is particularly useful in presence of heavy, independent tasks.

Documentation

Overview

Package parallel simplifies use of parallel (as not concurrent) workers that run on their own core. Number of workers is adjusted at runtime in base of numbers of cores. This paradigm is particularly useful in presence of heavy, independent tasks.

Example

Example shows example usage of the package.

package main

import (
	"fmt"
	"math"
	"runtime"

	"github.com/eraclitux/parallel"
)

type job struct {
	start   int
	stop    int
	results map[int]bool
}

func (j *job) Execute() {
	j.results = make(map[int]bool)
	for i := j.start; i <= j.stop; i++ {
		j.results[i] = isPrime(uint64(i))
	}
}

func isPrime(n uint64) bool {
	if n <= 2 {
		return true
	}
	var i uint64
	i = 2
	num := uint64(math.Sqrt(float64(n)))
	for i <= num {
		if n%i == 0 {
			return false
		}
		i++
	}
	return true
}

// Example shows example usage of the package.
func main() {
	cores := runtime.NumCPU()
	// Creates the slice of tasks that we want to execute in parallel.
	tasks := make([]parallel.Tasker, 0, cores)
	prev := 1
	// Bigger number to check.
	var limit int = 1e5
	// Create as much tasks as number of cores.
	d := int(limit / cores)
	for i := 1; i < limit; i++ {
		// This is not the best way to distribute load
		// as complexity is not the same in different
		// intervals (bigger numbers are more difficult to verify),
		// so some cores remains idle sooner.
		// We could increase efficiency making different interval lengths.
		if (i % d) == 0 {
			j := &job{start: prev, stop: i}
			prev = i + 1
			tasks = append(tasks, parallel.Tasker(j))
		}
	}
	// Do not forget last interval.
	j := &job{start: prev, stop: limit}
	tasks = append(tasks, parallel.Tasker(j))

	// Run tasks in parallel using all cores.
	err := parallel.Run(tasks)
	if err == nil {
		fmt.Println("Example OK")
	}

}
Output:

Example OK

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrTasksNotCompleted = errors.New("SIGINT received, not all tasks have been completed")

ErrTasksNotCompleted says that not all tasks where completed.

Functions

func Run

func Run(jobs []Tasker) (err error)

Run starts the goroutines that will execute Taskers. It is intended to run blocking in the main goroutine.

Types

type Tasker

type Tasker interface {
	Execute()
}

Tasker interface models an heavy task that have to be executed from a worker.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL