首页 > 其他分享 >Monte Carlo Estimation of Area Inside a Curve

Monte Carlo Estimation of Area Inside a Curve

时间:2024-09-14 11:24:11浏览次数:1  
标签:gSize Monte area Inside Area curve points threads your

Assignment 2
Due Sep 23 by 11:59pm Points 70 Submitting a file upload
Available Sep 13 at 10am - Dec 24 at 11:59pm
Start Assignment
Assignment 2 (70 Points)
Due Monday Sep 23 at 11:59 PM
In this assignment, you need to parallelize simple programs using C++11 threads. There are two
problems in this assignment, and for each problem you are provided the serial C++ implementation, the
expected parallelization strategy, and the expected output to be generated by your parallel solution.
Before starting this assignment, you should have completed the Slurm Tutorial
(https://canvas.sfu.ca/courses/84236/pages/slurm-tutorial) which walks you through how to use our
private cluster for your code development. [NOTE: The cluster is not set up to run Slurm yet, so
please start the assignment on CSIL machines. We will send an announcement when the cluster
is ready.]
General Instructions
1. You are provided with the serial version of all the programs at assignment2.tar.gz
(https://canvas.sfu.ca/courses/84236/files/24448336?wrap=1)
(https://canvas.sfu.ca/courses/84236/files/24448336/download?download_frd=1) . To run a program
(e.g., curve_area.cpp ), follow the steps below:
Run make curve_area . This creates a binary file called curve_area .
Create a slurm job to run the binary file using the following command: ./curve_area --nPoints
10000000 --coeffA 2.0 --coeffB 4.0? --rSeed 15
Use the command-line argument --nPoints to specify the number of points to be generated
(detailed description about curve_area mentioned below).
2. All parallel programs should have the command-line argument --nThreads to specify the number of
threads for the program. Example: --nThreads 4 .
3. While testing your solutions, make sure that cpus-per-task is correctly specified in your slurm
config file based on your requirement.
4. You will be asked to print the time spent by different threads on specific code regions. The time spent
by any code region can be computed as follows:
timer t1;
t1.start();
 Assignment 2
 1/6
/* ---- Code region whose time is to be measured --- */
double time_taken = t1.stop();
5. Sample outputs for all the programs can be found in sample_outputs directory. Programs will be
evaluated and graded automatically. Please make sure that your program output strictly
follows the sample output format.
6. We have provided test scripts for you to quickly test your solutions during your development process.
You can test your code using the test script available at test_scripts/ . Note that these test scripts
only validate the output formats, and a different evaluation script will be used for grading the
assignments. Important: You should use slurm when performing these and other tests. The test
scripts under test_scripts/ folder test for up to 8 threads; make sure --cpus-per-task=8 is set in
your slurm job.
$ ls test_scripts/*tester.pyc
curve_area_tester.pyc heat_transfer_tester.pyc
1. Monte Carlo Estimation of Area Inside a Curve [25 Points]
The area inside an arbitrary curve can be computed using the mechanism explained in class, similar to
the Monte Carlo Pi Estimation and Monte Carlo Ellipse Area Estimation. In this problem, you will
compute the area inside a curve with the following equation:
Where a and b are positive numbers. For example, the curve represented by is shown
here:
Another example: The curve represented by is shown here:
The method can be summarized in the following steps:
 Assignment 2
 2/6
1. Consider a curve that follows the above equation with coefficients a and b. For the purposes of this
problem, both coefficients are >= 1. With these coefficient values, the curve will be completely
enclosed inside a square with corner coordinates (-1,-1), (-1,1), (1,1), (1,-1).
2. The ratio of the curve area to the square area is determined by the relative count of points inside the
curve to total points inside the square.
3. We randomly generate n points inside the square, where both the x-coordinate and y-coordinate are
between -1 and 1. Let cpoints out of the n points fall inside the curve. A point is determined to be
inside the curve if 
4. The area inside the curve is then approximated as: Area / 4 = cpoints / n ==> Area = 4 *
cpoints / n .
The program below implements the above algorithm.
 uint curve_count = 0;
 double x_coord, y_coord;
 for (uint i = 0; i < n; i++) {
 x_coord = (2.0 * get_random_coordinate(&random_seed)) - 1.0);
 y_coord = (2.0 * get_random_coordinate(&random_seed)) - 1.0);
 if ( (a * (x^2)) + (b * (y^4)) ) <= 1.0)
 curve_count++;
 }
 double area = 4.0 * (double)curve_points / (double)n;
Your goal is to parallelize the above algorithm. Specifically, we are interested in parallelizing the for loop
such that each thread generates (approximately) n/T points, where T is the number of threads. Below
is the pseudo-code showing the logic of our parallel solution:
 Create T threads
 for each thread in parallel {
 Get the local_curve_count for (approximately) n/T points
 }
 total_curve_points = Accumulate the local_curve_counts from all threads ? ? 
 area = 4.0 *(double)total_curve_points / (double)n;
The serial implementation is available in curve_area.cpp . You have to parallelize the given serial
implementation using C++11 threads.
Your parallel solution must satisfy the following:
1. The file should be named curve_area_parallel.cpp .
2. Your program should accept the following parameters:
nThreads: Number of threads.
nPoints: Total number of points used for estimating the area. This number should be divided
equally among threads (with the remainder r=nPoints % nThreads going to threads 0,...,r-1 )
coeffA: Value of coefficient a.
coeffB: Value of coefficient b.
rSeed: Seed of the random number generator that you use in the program.
3. Your parallel solution must output the following information:
 Assignment 2
 3/6
Total number of threads used.
For each thread: the number of random points generated, the number of points within the curve,
and the time 代 写 Monte Carlo Estimation of Area Inside a Curve taken to generate and process these points (your threads should be numbered
between [0, T) ).
The total number of points generated.
The total number of points within the curve.
The total time taken for the entire execution (the code region to be timed is highlighted using
comments in the serial code).
4. The sample output can be found in sample_outputs/curve_area.output .
Please note that the output format should strictly match the expected format (including "spaces" and
"commas"). You can test your code using the test script as follows:
 $ python <absolute_path>/curve_area_tester.pyc --execPath=<absolute path of curve_area_parallel>
--scriptPath=<absolute path>/curve_area_evaluator.pyc
2. Heat Transfer [45 Points]
A description of the heat transfer problem was discussed in class, and is also available from the LLNL
Parallel Computing Tutorial here (https://hpc.llnl.gov/training/tutorials/introduction-parallel-computing?tutorial#ExamplesHeat) . The following code shows the basic serial implementation for this problem:
 // Initialize Temperature Array Prev[][]. Points in the middle are set to mTemp while rest is se
t to 0
 for (uint stepcount=1; stepcount <= tSteps; stepcount ++) {
 for (uint x = 0; x < gSize; x++) {
 for (uint (y = 0; y < gSize; y++) {
 // Compute new Temperature Array Curr[x][y] from Prev[][] values
 } // for y
 } // for x
 // swap Prev[][], Curr[][]
 } // for stepcount
 // Print temperature of certain points in Temperature Array 
Your goal is to parallelize the above algorithm such that each thread works on a vertical slice of the
array. Below is the pseudo-code showing the logic of the parallel solution:
 Create T threads
 for each thread in parallel {
 for (uint local_stepcount=1; local_stepcount <= tSteps; local_stepcount ++) {
 Compute the Temperature Array values Curr[][] in the slice allocated to this thread from Pre
v[][] 
 Barrier(); // all threads need to finish current time step
 if (this is thread 0) { swap Curr[][], Prev[][]; Barrier(); }
 else Barrier(); // wait till thread 0 is done with the swap before moving to next time step 
 } // for local_stepcount 
 } // for each thread
 // Print temperatures of points of interest
The serial implementation is available in heat_transfer.cpp . You have to parallelize the given serial
implementation using C++11 threads. For your parallel code, you can use the custom barrier
 Assignment 2
 4/6
implementation available in core/utils.h or you can implement your own
1. The file should be named heat_transfer_parallel.cpp . Your program should accept the following
parameters:
nThreads: Total number of threads used.
gSize: Grid size. The size of the temperature array is gSize x gSize.
mTemp: Temperature values in the middle of the array, from [gSize/3 , gSize/3] to [2*gSize/3 ,
2*gSize/3].
iCX: Coefficient of horizontal heat transfer.
iCY: Coefficient of vertical heat transfer.
tSteps: Time steps of the simulation
2. Your parallel solution must output the following information:
Grid Size.
Total number of threads used.
Values of iCX, iCY, mTemp and tSteps
For each thread: thread id, start column, end column, time taken.
Temperatures at end of simulation for points at [0,0], [gSize/6, gSize/6], [gSize/3, gSize/3],
[gSize/2, gSize/2], [2*gSize/3, 2*gSize/3], [5*gSize/6, 5*gSize/6].
Temperatures at the right boundary of all threads: [endx[0], endx[0]], [endx[1], endx[1]], ...,
[[endx[nThreads-1],endx[nThreads-1]].
The total time taken for the entire execution (the code region to be timed is highlighted using
comments in the serial code).
3. The sample console output can be found in sample_outputs/heat_transfer.output .
Please note that the output format should strictly match the expected format (including "spaces" and
"commas"). You can test your code using the test script as follows:
 $ python <path to your directory>/test_scripts/heat_transfer_tester.pyc --execPath=<absolute pat
h of heat_tranfer_parallel> --scriptPath=<absolute path of heat_transfer_evaluator.pyc>
Submission Guidelines
Make sure that your solutions folder has the following files and sub-folders. Let's say your solutions
folder is called my_assignment2_solutions . It should contain:
core/ -- The folder containing all core files. It is already available in the assignment package. Do
not modify it or remove any files.
Makefile -- Makefile for the assignment. This file should not be changed.
curve_area_parallel.cpp
heat_transfer_parallel.cpp
To create the submission file, follow the steps below:
1. Enter in your solutions folder, and remove all the object/temporary files.
 Assignment 2
 5/6
$ cd my_assignment2_solutions/
$ make clean
2. Create the tar.gz file.
$ tar cvzf assignment2.tar.gz *
which creates a compressed tar ball that contains the contents of the folder.
3. Validate the tar ball using the submission_validator.pyc script.
$ python <path to your directory>/test_scripts/submission_validator.pyc --tarPath=<absolute p
ath>/assignment2.tar.gz
Submit via canvas by the deadline.
 Assignment 2

标签:gSize,Monte,area,Inside,Area,curve,points,threads,your
From: https://www.cnblogs.com/qq---99515681/p/18413612

相关文章

  • OpenCV结构分析与形状描述符(19)查找二维点集的最小面积外接旋转矩形函数minAreaRect()
    操作系统:ubuntu22.04OpenCV版本:OpenCV4.9IDE:VisualStudioCode编程语言:C++11算法描述找到一个包围输入的二维点集的最小面积旋转矩形。该函数计算并返回指定点集的最小面积边界矩形(可能是旋转的)。开发者需要注意的是,当数据接近包含的Mat元素边界时,返回的Rotated......
  • Monte Carlo方法解决强化学习问题
    本文继续深入探讨蒙特卡罗(MC)方法。这些方法的特点是能够仅从经验中学习,不需要任何环境模型,这与动态规划(DP)方法形成对比。这一特性极具吸引力-因为在实际应用中,环境模型往往是未知的,或者难以精确建模转移概率。以21点游戏为例:尽管我们完全理解游戏规则,但通过DP方法解......
  • VLAN (Virtual Local Area Network)虚拟局域网
    1.1基本概念        虚拟局域网(VLAN)是一组逻辑上的设备和用户,这些设备和用户并不受物理位置的限制,可以根据功能、部门及应用等因素将它们组织起来,相互之间的通信就好像它们在同一个网段中一样,由此得名虚拟局域网,由于交换机端口有两种VLAN属性,其一是VLANID,其二是VLANT......
  • INSIDE: LLMS’ INTERNAL STATES RETAIN THE POWER OF HALLUCINATION DETECTION
    本文是LLM系列文章,针对《INSIDE:LLMS’INTERNALSTATESRETAINTHEPOWEROFHALLUCINATIONDETECTION》的翻译。INSIDE:LLMS的内部状态保留了幻觉检测的力量摘要1引言2幻觉检查的背景3方法4实验5相关工作6结论摘要知识幻觉引起了人们对部署的LLM的......
  • 局域网(Local Area Network,简称LAN)
    局域网(LocalAreaNetwork,简称LAN)是指在一个相对较小的范围内(如一个办公室、学校或家庭)内的计算机和其他网络设备通过网络设备(如交换机、路由器等)互相连接起来,共享资源和信息的网络系统。局域网的主要特点如下:范围有限:局域网通常覆盖一个有限的地理范围,如一个建筑物或一个......
  • (1) 定义一个Circle类,包含一个double型的radius属性代表圆的半径,findArea()方法返回圆
    1publicclassHomework13{2//编写一个mian方法3publicstaticvoidmain(String[]args){4Circlec=newCircle();5PassObjectpo=newPassObject();6po.printAreas(c,5);78}9}101112classCircle{13......
  • C# read excel file via ExcelDataReader
    Install-PackageExcelDataReader usingExcelDataReader;usingSystem.Text;usingSystem.IO;namespaceConsoleApp50{internalclassProgram{staticvoidMain(string[]args){ReadExcel("aaa.xls");......
  • web前端之根据字符串长度从长到短排序、中文字符串优先、样式循环、禁止冒泡、悬浮、
    MENU前言效果图htmlstyleJavaScript前言1、代码段由HTML、CSS(使用Sass语法)和JavaScript组成,创建一个文本框,用户可以在其中输入内容,并通过点击按钮进行操作。2、代码段的主要功能是允许用户输入一系列以、分隔的项,并根据长度对这些项进行排序(中文字符优先),然后......
  • 面试题:在Java中,JVM(Java虚拟机)的内存模型是如何设计的?请详细解释堆(Heap)、栈(Stack)、方法
    面试题:在Java中,JVM(Java虚拟机)的内存模型是如何设计的?请详细解释堆(Heap)、栈(Stack)、方法区(MethodArea)以及程序计数器(ProgramCounterRegister)的作用和它们之间的关系。更多答案在这里,手机或电脑浏览器就可以打开,面霸宝典【全拼音】.com这里可以优化简历,模拟面试,企业项......
  • ALTER TABLE area_biz_map_front ALTER COLUMN id SERIAL
     ALTERTABLEarea_biz_map_frontALTERCOLUMNidSERIAL您的问题看起来是想在已存在的表中为id列添加自增序列。不过,您的SQL语法是针对PostgreSQL的,而在MySQL中,SERIAL关键字被用来创建新列,并自动为其设置自增属性。如果你正在使用MySQL,你可以使用以下语句: ......