Skip to content

Commit

Permalink
Add ParallelTable.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Oct 15, 2018
1 parent d18b2b2 commit f60788b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
29 changes: 29 additions & 0 deletions expreduce/builtin_list.go
Expand Up @@ -3,6 +3,7 @@ package expreduce
import "bytes"
import "math/big"
import "sort"
import "sync"

func (this *Expression) ToStringList(params ToStringParams) (bool, string) {
if params.form == "FullForm" {
Expand Down Expand Up @@ -203,6 +204,34 @@ func GetListDefinitions() (defs []Definition) {
return this
},
})
defs = append(defs, Definition{
Name: "ParallelTable",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) >= 3 {
mis, isOk := multiIterSpecFromLists(es, this.Parts[2:])
if isOk {
// Simulate evaluation within Block[]
toReturn := NewExpression([]Ex{NewSymbol("System`List")})
for mis.cont() {
toReturn.Parts = append(toReturn.Parts, ReplacePD(this.Parts[1].DeepCopy(), es, mis.currentPDManager()))
es.Debugf("%v\n", toReturn)
mis.next()
}
var wg sync.WaitGroup
for i := 1; i < len(toReturn.Parts); i++ {
wg.Add(1)
go func (idx int) {
defer wg.Done()
toReturn.Parts[idx] = toReturn.Parts[idx].Eval(es)
}(i)
}
wg.Wait()
return toReturn
}
}
return this
},
})
defs = append(defs, Definition{
Name: "MemberQ",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
Expand Down
8 changes: 8 additions & 0 deletions expreduce/iterspec.go
Expand Up @@ -211,6 +211,14 @@ func (this *multiIterSpec) defineCurrent(es *EvalState) {
}
}

func (this *multiIterSpec) currentPDManager() *PDManager {
pm := &PDManager{make(map[string]Ex)}
for i := range this.iSpecs {
pm.patternDefined[this.iSpecs[i].getIName()] = this.iSpecs[i].getCurr()
}
return pm
}

func (this *Expression) evalIterationFunc(es *EvalState, init Ex, op string) Ex {
if len(this.Parts) >= 3 {
mis, isOk := multiIterSpecFromLists(es, this.Parts[2:])
Expand Down
8 changes: 4 additions & 4 deletions expreduce/resources.go

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions expreduce/resources/list.m
Expand Up @@ -56,6 +56,19 @@
]
};

ParallelTable::usage = "`ParallelTable[expr, n]` returns a list with `n` copies of `expr`, evaluated in parallel.
`ParallelTable[expr, {sym, n}]` returns a list with `expr` evaluated with `sym` = 1 to `n`, evaluated in parallel.
`ParallelTable[expr, {sym, m, n}]` returns a list with `expr` evaluated with `sym` = `m` to `n`, evaluated in parallel.";
ParallelTable[a_, b_Integer] := ParallelTable[a, {UniqueDefined`i, 1, b}];
Attributes[ParallelTable] = {HoldAll, Protected};
Tests`ParallelTable = {
ESimpleExamples[
ESameTest[{5, 6, 7, 8, 9, 10}, ParallelTable[i, {i, 5, 10}]],
]
};

MemberQ::usage = "`MemberQ[expr, pat]` returns True if any of the elements in `expr` match `pat`, otherwise returns False.";
Attributes[MemberQ] = {Protected};
Tests`MemberQ = {
Expand Down

0 comments on commit f60788b

Please sign in to comment.