Mathematica中文论坛-非官方

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 7028|回复: 0
打印 上一主题 下一主题

matlab parfor pool 多核 并行 example 二层for

[复制链接]

532

主题

603

帖子

3035

积分

论坛元老

Rank: 8Rank: 8

积分
3035
跳转到指定楼层
楼主
发表于 2016-3-9 20:58:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 meatball1982 于 2016-3-9 21:45 编辑

实际上,matlab自身的并行已经挺好了.
许多的函数,不用设置,就可以并行.但有时,有些东西要自己写.

我的机器是四核的.打开两个,剩下一个用来控制,(也可以打开四个,进程里就有5个matlab了.)
我的matlab
8.5.0.197613 (R2015a)
注意,符值那为什么不行.因为floor?

二层for的话,用一个ind代替.
用parfor来算,再转化.

  1. clear all
  2. clc

  3. poolobj=parpool('local',2)

  4. parfor i=1:8
  5.     i
  6.     i*2-1.5
  7.     floor(i*2-1.5)
  8.     disp('----this should have smothing below ')
  9.     j=floor(i*2-1.5)
  10.     disp('----this should have smothing up')

  11. end

  12. delete(poolobj)
复制代码



复制代码

  1. Starting parallel pool (parpool) using the 'local' profile ... connected to 2 workers.

  2. poolobj =

  3. Pool with properties:

  4.             Connected: true
  5.            NumWorkers: 2
  6.               Cluster: local
  7.         AttachedFiles: {}
  8.           IdleTimeout: 30 minute(s) (30 minutes remaining)
  9.           SpmdEnabled: true


  10. ans =

  11.      6


  12. ans =

  13.    10.5000


  14. ans =

  15.     10

  16. ----this should have smothing below
  17. ----this should have smothing up

  18. ans =

  19.      5


  20. ans =

  21.     8.5000


  22. ans =

  23.      8

  24. ----this should have smothing below
  25. ----this should have smothing up

  26. ans =

  27.      4


  28. ans =

  29.     6.5000


  30. ans =

  31.      6

  32. ----this should have smothing below
  33. ----this should have smothing up

  34. ans =

  35.      3


  36. ans =

  37.     4.5000


  38. ans =

  39.      4

  40. ----this should have smothing below
  41. ----this should have smothing up

  42. ans =

  43.      2


  44. ans =

  45.     2.5000


  46. ans =

  47.      2

  48. ----this should have smothing below
  49. ----this should have smothing up

  50. ans =

  51.      1


  52. ans =

  53.     0.5000


  54. ans =

  55.      0

  56. ----this should have smothing below
  57. ----this should have smothing up

  58. ans =

  59.      7


  60. ans =

  61.    12.5000


  62. ans =

  63.     12

  64. ----this should have smothing below
  65. ----this should have smothing up

  66. ans =

  67.      8


  68. ans =

  69.    14.5000


  70. ans =

  71.     14

  72. ----this should have smothing below
  73. ----this should have smothing up
  74. Parallel pool using the 'local' profile is shutting down.
复制代码
好吧.有学习了一点.下面是一个比较
01单CPU两层for,
02.多CPU两层for,先parfor 再for.
03.多CPU两层for ,先for 再parfor

如果求解的是一个对称matrix,则可以人尝试方法03.

  1. clear all
  2. clc

  3. poolobj=parpool('local',4)

  4. % parfor i=1:8
  5. %     i
  6. %     i*2-1.5
  7. %     floor(i*2-1.5)
  8. %     disp('----this should have smothing below ')
  9. %     j=floor(i*2-1.5)
  10. %     disp('----this should have smothing up')
  11. %
  12. % end

  13. n=6
  14. %% cpu version
  15. tic
  16. M1 = magic(n);
  17. for x = 1:n
  18.     for y = 1:n
  19.         M2(x,y) = x*10 + y + M1(x,y)/10000;
  20.     end
  21. end
  22. M2;
  23. toc

  24. %% multi cpu version 2 layer loop
  25. % parfor out
  26. tic
  27. M3 = magic(n);
  28. M4 = zeros(n);
  29. parfor a = 1:n
  30.     for b = 1:n
  31.         M4(a,b) = a*10 + b + M3(a,b)/10000;
  32.     end
  33. end
  34. M4;
  35. toc

  36. %% multi cpu version 2 layer loop
  37. % for out
  38. tic
  39. M5 = magic(n);
  40. M6 =zeros(n);

  41. for a = 1:n
  42.     parfor b = a:n
  43.         M6(a,b) = a*10 + b + M5(a,b)/10000;
  44.     end
  45. end
  46. M7=M6+M6';
  47. toc

  48. delete(poolobj)
复制代码


  1. <blockquote>clear all
复制代码





分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|Mathematica中文论坛-非官方 ( 辽ICP备16001491号-1

GMT+8, 2024-5-1 01:43 , Processed in 0.120163 second(s), 25 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表