简体   繁体   English

Koch雪花渲染时间(以及如何使用乌龟绘制雪花)

[英]Koch snowflake rendering time (and how to draw a snowflake using turtle)

I'm currently working through the online course material for the MIT 6.006 course for fun. 我目前正在研究MIT 6.006课程的在线课程材料,很有趣。 I'm on problem set #2 (found here ) and had a question about the calculations for the asymptotic rendering time for the koch snowflake problem (problem #1). 我在问题集2(在此处找到)上,对科赫雪花问题(问题1)的渐近渲染时间的计算存在疑问。

According to the solutions, when the CPU is responsible for the rendering and the calculation of coordinates, the asymptotic rendering time is faster than if the process is split up between the CPU and GPU. 根据这些解决方案,当CPU负责渲染和坐标计算时,渐进渲染时间要比在CPU和GPU之间进行拆分的过程要快。 The math makes sense to me, but does anyone have an intuition about why this is true? 数学对我来说很有意义,但是有人对它为什么如此有直觉吗?

In my mind, the CPU still has to calculate the coordinates to render the snowflake (Theta(4^n) time), and then has to render the image. 在我看来,CPU仍然必须计算坐标以渲染雪花(Theta(4 ^ n)时间),然后必须渲染图像。 In my mind, these should be additive, not multiplicative. 在我看来,这些应该是加性的,而不是乘法性的。

However, the solutions state these are multiplicative, so since each triangle/line segment is shorter (for the last two subproblems in problem 1) the runtime is reduced to either Theta((4/3)^n) or Theta(1)! 但是,解表明它们是可乘的,因此,由于每个三角形/线段都较短(对于问题1中的最后两个子问题),运行时间减少为Theta((4/3)^ n)或Theta(1)!

I'm not a computer scientist--this stuff is just a fun hobby for me. 我不是计算机科学家,这对我来说只是个有趣的爱好。 I'd really appreciate an answer from one of you geniuses out there :) 我真的很感谢你们其中一位天才的回答:)

Also, some fun I had while playing with the python turtle module. 另外,我在玩python turtle模块时获得了一些乐趣。 Heres some very imperfect code to draw a koch snowflake in python: 以下是一些非常不完善的代码,它们在python中绘制了科赫雪花:

import turtle

def snowflake(n,size=200):
    try: turtle.clear()
    except: pass
    turtle.tracer(0,0)
    snowflake_edge(n,size)
    turtle.right(120)
    snowflake_edge(n,size)
    turtle.right(120)
    snowflake_edge(n,size)
    turtle.update()
    turtle.hideturtle()

def snowflake_edge(n,size=200):
    if n==0:
        turtle.forward(size)
    else:
        snowflake_edge(n-1,size/3.0)
        turtle.left(60)
        snowflake_edge(n-1,size/3.0)
        turtle.right(120)
        snowflake_edge(n-1,size/3.0)
        turtle.left(60)
        snowflake_edge(n-1,size/3.0)

As indicated by the comments on P.5 of the problem set, the approach taken by the CPU and the GPU are different. 如问题集第5页的注释所示,CPU和GPU采取的方法不同。

The CPU-only (without hardware acceleration) appproach is to first compute what needs to be drawn, and then send it to the GPU to draw. 仅使用CPU(无硬件加速)的方法是首先计算需要绘制的内容,然后将其发送到GPU进行绘制。 Since we are assuming that the cost to rasterize is bigger than the cost to gather the line segments, then the amount of time to draw the image will be dominated by the GPU, whose cost will be proportional to the number of pixels it actually has to draw. 由于我们假设栅格化的成本大于收集线段的成本,因此绘制图像的时间将由GPU决定,其成本与实际需要的像素数量成比例画。

The GPU-CPU (with hardware acceleration) approach computes all the triangles, big and small, and then sends them to the GPU to draw the big triangle, delete the middle pixels, draw the smaller triangles, delete the unneeded pixels, and so on. GPU-CPU(具有硬件加速)方法可计算所有大小的三角形,然后将它们发送到GPU,以绘制大三角形,删除中间像素,绘制较小的三角形,删除不需要的像素,依此类推。 Since drawing takes a long time, then the time GPU has to spend drawing and erasing will dominate the total amount of time spent; 由于绘图需要很长时间,因此GPU必须花费的绘图和擦除时间将占总时间。 since most (asymptotically, 100%) of the pixels that are drawn will in the end be erased, then the total amount of time taken will be substantially more than simply the number of pixels that actually have to be drawn. 由于最后将擦除大多数(渐近地为100%)像素,因此所花费的总时间将大大超过实际必须绘制的像素数量。

In other words: the hardware-acceleration scenario dumps most of the work on to the GPU, which is much slower than the CPU. 换句话说:硬件加速方案将大部分工作转储到GPU上,这比CPU慢得多。 This is okay if the CPU has other things to work on while the GPU is doing its processing; 如果在GPU处理过程中CPU还有其他事情需要处理,这没关系; however, this is not the case here; 但是,这里不是这种情况。 so, the CPU is just wasting cycles while the GPU is doing its drawing. 因此,CPU在浪费时间而GPU正在绘制图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM