我正在使用带有Scala插件的JetBrains IntelliJ IDEA,我在main/scala/myparallel
目录中有一个package.scala文件,其中包含以下代码:
package myparallel
import java.util.concurrent._
import scala.util.DynamicVariable
object primitives {
def time[R](block: => R): R = {
val t0 = System.nanoTime()
val result = block // call-by-name
val t1 = System.nanoTime()
println("Elapsed time: " + (t1 - t0)/1000000 + "ms")
result
}
val forkJoinPool = new ForkJoinPool
abstract class TaskScheduler {
def schedule[T](body: => T): ForkJoinTask[T]
def parallel[A, B](taskA: => A, taskB: => B): (A, B) = {
val right = task {
taskB
}
val left = taskA
(left, right.join())
}
}
class DefaultTaskScheduler extends TaskScheduler {
def schedule[T](body: => T): ForkJoinTask[T] = {
val t = new RecursiveTask[T] {
def compute = body
}
forkJoinPool.execute(t)
t
}
}
val scheduler =
new DynamicVariable[TaskScheduler](new DefaultTaskScheduler)
def task[T](body: => T): ForkJoinTask[T] = {
scheduler.value.schedule(body)
}
def parallel[A, B](taskA: => A, taskB: => B): (A, B) = {
scheduler.value.parallel(taskA, taskB)
}
def parallel[A, B, C, D](taskA: => A, taskB: => B, taskC: => C, taskD: => D): (A, B, C, D) = {
val ta = task { taskA }
val tb = task { taskB }
val tc = task { taskC }
val td = taskD
(ta.join(), tb.join(), tc.join(), td)
}
}
我通过将import myparallel.primitives._
导入此代码,在各种.sc工作表文件中使用此代码,例如以下示例:
import myparallel.primitives._
import scala.util.Random
val threshold = 10000
def mapArrPar[A,B](inp: Array[A], left: Int, right: Int,
f : A => B, out: Array[B]): Unit = {
if (right - left < threshold)
mapArr(inp, left, right, f, out)
else {
val mid = left + (right - left)/2
parallel(mapArrPar(inp, left, mid, f, out),
mapArrPar(inp, mid, right, f, out))
}
}
val vectIn = Array.fill(100000)(1).map(_ * scala.util.Random.nextInt)
val vectOut = new Array[Double](100000)
time(mapArrPar(vectIn, 0, vectIn.length, (x:Int) => Math.pow(x, 19), vectOut))
lint不会给我任何错误,但是如果我尝试执行工作表,则会遇到一系列错误,第一个错误是Error:not found: value myparallel
import myparallel.primitives._
。此外,每次使用前面提到的程序包中定义的功能都存在其他错误,例如Error:(51, 6) not found: value parallel
parallel(mapArrPar(inp, left, mid, f, out),
包或导入语句有什么问题?