我需要在EREW PRAM系统上的并行计算中编写 factorial function (n!)。 假设我们有n个proccessors。 复杂性应该是log n。 我怎么能这样做?
答案 0 :(得分:2)
我们有n个proccessors。复杂性应该是log n。
这个问题毫无意义,因为你正在寻找一种算法,当你添加处理器时复杂度(log n
)增加(即增加n
)。
我猜你要做的是将产品1*2*3*...*k
拆分成相同大小的n
块,在单独的处理器上计算每个子产品,然后乘以{{ 1}}结果一起。
答案 1 :(得分:2)
通常,您可以为N个处理器划分工作N次,并单独计算每个处理器。您可以通过将每个工作的答案相乘来组合结果。例如第一个任务执行m !,下一个(2米)!/ m !,第三个任务(3米!)/(2米!)等。当你得到多个结果时你得到n!。
BTW:你不会为n
的小值(例如小于1000)执行此操作,因为启动新线程/任务的开销可能大于在单个线程中执行此操作所需的时间。< / p>
我怀疑伪代码是不够的所以这是一个例子
public enum CalcFactorial {;
public static BigInteger factorial(long n) {
BigInteger result = BigInteger.ONE;
for (long i = 2; i <= n; i++)
result = result.multiply(BigInteger.valueOf(i));
return result;
}
public static BigInteger pfactorial(long n) {
int processors = Runtime.getRuntime().availableProcessors();
if (n < processors * 2)
return factorial(n);
long batchSize = (n + processors - 1) / processors;
ExecutorService service = Executors.newFixedThreadPool(processors);
try {
List<Future<BigInteger>> results = new ArrayList<Future<BigInteger>>();
for (long i = 1; i <= n; i += batchSize) {
final long start = i;
final long end = Math.min(n + 1, i + batchSize);
results.add(service.submit(new Callable<BigInteger>() {
@Override
public BigInteger call() throws Exception {
BigInteger n = BigInteger.valueOf(start);
for (long j = start + 1; j < end; j++)
n = n.multiply(BigInteger.valueOf(j));
return n;
}
}));
}
BigInteger result = BigInteger.ONE;
for (Future<BigInteger> future : results) {
result = result.multiply(future.get());
}
return result;
} catch (Exception e) {
throw new AssertionError(e);
} finally {
service.shutdown();
}
}
}
public class CalcFactorialTest {
@Test
public void testFactorial() {
final int tests = 200;
for (int i = 1; i <= tests; i++) {
BigInteger f1 = factorial(i * i);
BigInteger f2 = pfactorial(i * i);
assertEquals(f1, f2);
}
long start = System.nanoTime();
for (int i = 1; i <= tests; i++) {
BigInteger f1 = factorial(i * i);
}
long mid = System.nanoTime();
for (int i = 1; i <= tests; i++) {
BigInteger f2 = pfactorial(i * i);
}
long end = System.nanoTime();
System.out.printf("Single threaded took %.3f sec, multi-thread took %.3f%n",
(mid - start) / 1e9, (end - mid) / 1e9);
}
}
在3.72 GHz i7打印
Single threaded took 58.702 sec, multi-thread took 11.391