指令级并行是什么

指令级并行(ILP)指通过通过流水线等技术实现多条指令同时并行执行的并行技术
实现ILP主要的方法有:

  • 依靠硬件动态发现和开发并行
  • 依靠软件在编译时静态发现并行

    实例

    引入pom
    使用jcstress来进行大数据量测试

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.openjdk.jcstress</groupId>
    4. <artifactId>jcstress-core</artifactId>
    5. <version>${jcstress.version}</version>
    6. </dependency>
    7. </dependencies>
    8. <properties>
    9. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    10. <!--
    11. jcstress version to use with this project.
    12. -->
    13. <jcstress.version>0.5</jcstress.version>
    14. <!--
    15. Java source/target to use for compilation.
    16. -->
    17. <javac.target>1.8</javac.target>
    18. <!--
    19. Name of the test Uber-JAR to generate.
    20. -->
    21. <uberjar.name>jcstress</uberjar.name>
    22. </properties>
    23. <build>
    24. <plugins>
    25. <plugin>
    26. <groupId>org.apache.maven.plugins</groupId>
    27. <artifactId>maven-compiler-plugin</artifactId>
    28. <version>3.1</version>
    29. <configuration>
    30. <compilerVersion>${javac.target}</compilerVersion>
    31. <source>${javac.target}</source>
    32. <target>${javac.target}</target>
    33. </configuration>
    34. </plugin>
    35. <plugin>
    36. <groupId>org.apache.maven.plugins</groupId>
    37. <artifactId>maven-shade-plugin</artifactId>
    38. <version>2.2</version>
    39. <executions>
    40. <execution>
    41. <id>main</id>
    42. <phase>package</phase>
    43. <goals>
    44. <goal>shade</goal>
    45. </goals>
    46. <configuration>
    47. <finalName>${uberjar.name}</finalName>
    48. <transformers>
    49. <transformer
    50. implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    51. <mainClass>org.openjdk.jcstress.Main</mainClass>
    52. </transformer>
    53. <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
    54. <resource>META-INF/TestList</resource>
    55. </transformer>
    56. </transformers>
    57. </configuration>
    58. </execution>
    59. </executions>
    60. </plugin>
    61. </plugins>
    62. </build>

    I_Result是结果集,ACCEPTABLE_INTERESTING是我们推测的最极端条件下产生的结果,也就是可见性
    。 ```java import org.openjdk.jcstress.annotations.*; import org.openjdk.jcstress.infra.results.I_Result;

@JCStressTest @Outcome(id = {“1”, “4”}, expect = Expect.ACCEPTABLE, desc = “ok”) @Outcome(id = “0”, expect = Expect.ACCEPTABLE_INTERESTING, desc = “!!!!”) @State public class ConcurrencyTest {

  1. int num = 0;
  2. boolean ready = false;
  3. @Actor
  4. public void actor1(I_Result r) {
  5. if(ready) {
  6. r.r1 = num + num;
  7. } else {
  8. r.r1 = 1;
  9. }
  10. }
  11. @Actor
  12. public void actor2(I_Result r) {
  13. num = 2;
  14. ready = true;
  15. }

}

  1. 打包后<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/22478710/1647189067543-496a76b7-d39f-4ef3-876e-f715a4a0307d.png#clientId=u1774d5b7-86f9-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=369&id=u3a935e9e&margin=%5Bobject%20Object%5D&name=image.png&originHeight=581&originWidth=840&originalType=binary&ratio=1&rotation=0&showTitle=false&size=156524&status=done&style=none&taskId=u3657f65d-f552-4361-87de-1f334b68f99&title=&width=533.3333575536345)<br />在CMD中输入 java -jar target/jcstress.jar -t cn.itcast.ConcurrencyTest<br />触发了58次这个可见性的结果<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/22478710/1647189347813-b06a112d-5579-47c9-b9b5-0d2154a0ed11.png#clientId=u1774d5b7-86f9-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=244&id=u752a18af&margin=%5Bobject%20Object%5D&name=image.png&originHeight=384&originWidth=1702&originalType=binary&ratio=1&rotation=0&showTitle=false&size=282165&status=done&style=none&taskId=uec72d4a0-bec9-45bb-969a-da375157305&title=&width=1080.6349697098642)
  2. <a name="XQJNX"></a>
  3. # 解决方案
  4. ```java
  5. import org.openjdk.jcstress.annotations.*;
  6. import org.openjdk.jcstress.infra.results.I_Result;
  7. @JCStressTest
  8. @Outcome(id = {"1", "4"}, expect = Expect.ACCEPTABLE, desc = "ok")
  9. @Outcome(id = "0", expect = Expect.ACCEPTABLE_INTERESTING, desc = "!!!!")
  10. @State
  11. public class ConcurrencyTest {
  12. int num = 0;
  13. volatile boolean ready = false;
  14. @Actor
  15. public void actor1(I_Result r) {
  16. if(ready) {
  17. r.r1 = num + num;
  18. } else {
  19. r.r1 = 1;
  20. }
  21. }
  22. @Actor
  23. public void actor2(I_Result r) {
  24. num = 2;
  25. ready = true;
  26. }
  27. }

重新运行后:
就没有出现可见性问题了。
image.png