Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow GPU-based shading calculations #7302

Merged
merged 61 commits into from
Feb 21, 2020
Merged

Allow GPU-based shading calculations #7302

merged 61 commits into from
Feb 21, 2020

Conversation

nealkruis
Copy link
Member

@nealkruis nealkruis commented May 20, 2019

Add the ability to use GPU-based shading calculations using the pixel counting methodology. See the design doc.

Review Checklist

  • Functional code review (it has to work!)
  • CI status: all green or justified
  • Performance: CI Linux results include performance check
  • Unit Test(s)
  • C++ checks:
    • Argument types
    • If any virtual classes, ensure virtual destructor included, other things
  • IDD changes:
    • Verify naming conventions and styles, memos and notes and defaults
    • Open windows IDF Editor with modified IDD to check for errors
    • If transition, add to input rules file for interfaces
    • If transition, add transition source
    • If transition, update idfs
  • If new idf included, locally check the err file and other outputs
  • Required documentation updates?
  • If output changes, including tabular output structure, add to output rules file for interfaces

@nealkruis nealkruis added NewFeature Includes code to add a new feature to EnergyPlus IDDChange Code changes impact the IDD file (cannot be merged after IO freeze) labels May 20, 2019
@nealkruis nealkruis self-assigned this May 20, 2019

### Interior Solar Distribution ###

We will count pixels when viewing the from the perspective of the sun through a window with each internal surface assigned a different color, then use the histogramming functions in OpenGL to report the number of pixels of each color that are visible. This functionality will be added to Penumbra.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this place a limit on the maximum number of surfaces (e.g. maximum number of colors)?

Copy link

@macumber macumber May 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will this interior calculation work with blinds and shades on windows that are controlled at each timestep? What about shading surfaces with transmittance schedules.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to check, but I think we aren't limited to the number of colors. Even still, 255x255x255 = 16,581,375. Hard to imagine there would be more surfaces in a model (let alone a single zone).

Blinds/shades will work the same as they currently do. They aren't part of the shading calculations they affect the transmittance (of the solar through the window).

Scheduled transmittance is a different challenge discussed below in the document.

\note Advanced Feature. Internal default is SutherlandHodgman
\note Refer to InputOutput Reference and Engineering Reference for more information
\type choice
\key ConvexWeilerAtherton
\key SutherlandHodgman
\default SutherlandHodgman
A3 , \field Sky Diffuse Modeling Algorithm
A4 , \field Sky Diffuse Modeling Algorithm

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any parameters needed to control the pixel counting algorithm? I would think that number of pixels would be a parameter?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would an option to write out images or renderings be useful for debugging? Ability to write out shading schedules? Any other reporting that should be considered?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding a resolution input would be good. The rendering is mapped to a square, so maybe:

\field Pixel Counting Resolution
\note Number of pixels in both dimensions of the surface rendering
\type integer
\default 512

The library has the ability to open a window rendering of the scene, but it is currently used only for debugging the C++ code (not for general simulations). This could be added later if needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "Pixel Maximum Length" instead? Using the same number of pixels on a small surface as a large surface seems unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the pixel counting method cover diffuse shading?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method stretches any surface, regardless of its size, to fill as much of the rendered view as possible and provide the maximum possible resolution. What is rendered is often stretched in the X or Y direction to fit within the square of the determined size (that is, even a rectangular surface will be stretched to an aspect ratio of 1:1). The algorithm is aware that the projected dimensions are not necessarily equal and accounts for this appropriately.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, diffuse shading is done using the pixel counting method for each location in the sky dome grid.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So - does this field have any impact when using the GPU method? Ultimately, when this is complete, IDD notes and I/O ref will need to highlight which fields apply only to certain shading methods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will still have an impact. Ideally we will figure out how to handle variable transmittance surfaces and this will be handled the same way it is for the polygon clipping method.

@JasonGlazer
Copy link
Contributor

Have you done any benchmarking?

\type choice
\key ScheduledShading
\key InternalCalculation
\key ImportedShading
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you proposing to remove this field "External Shading Calculation Method"? We need to keep it to import shading calculation results by external tools.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hongtz68 I'm not proposing to remove it. I'm proposing moving the capability to a new field: "Shading Calculation Method"

\note Advanced Feature. Internal default is SutherlandHodgman
\note Refer to InputOutput Reference and Engineering Reference for more information
\type choice
\key ConvexWeilerAtherton
\key SutherlandHodgman
\default SutherlandHodgman
A3 , \field Sky Diffuse Modeling Algorithm
A4 , \field Sky Diffuse Modeling Algorithm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "Pixel Maximum Length" instead? Using the same number of pixels on a small surface as a large surface seems unnecessary.

@@ -526,18 +540,18 @@ ShadowCalculation,
\note only really applicable to RunPeriods
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is misleading, because SizingPeriod:WeatherFileDays would also use this. I'd probably just delete this note

For the notes above this line . . .
AverageOverDaysInFrequency --> Periodic
0=Use Default Periodic Calculation (minimum is 1, so zero isn't valid since who know when).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. This is all existing text, though I can clean it up here.

\note Advanced Feature. Internal default is SutherlandHodgman
\note Refer to InputOutput Reference and Engineering Reference for more information
\type choice
\key ConvexWeilerAtherton
\key SutherlandHodgman
\default SutherlandHodgman
A3 , \field Sky Diffuse Modeling Algorithm
A4 , \field Sky Diffuse Modeling Algorithm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the pixel counting method cover diffuse shading?

\note If Imported is chosen, the Schedule:File:Shading object is required.
\type choice
\key PolygonClipping
\key PixelCounting
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"GPUPixelCounting"?

CMakeLists.txt Outdated
@@ -25,6 +25,7 @@ set( ENERGYPLUS_VERSION "${CMAKE_VERSION_MAJOR}.${CMAKE_VERSION_MINOR}.${CMAKE_V

set( CMAKE_VERSION_BUILD "Unknown" CACHE STRING "Build number" )
find_package(Git)
find_package(OpenGL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nealkruis Is this requiring that OpenGL be available to build? If so, wouldn't it be better to have a fallback that doesn't require the availability of OpenGL?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put this in here with the idea that I would disable some of the unit tests if the machine doesn't have OpenGL. I'm not sure it's really necessary though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure we're all clear of the requirements on this.

We will not make OpenGL, or any other graphical rendering library, a hard requirement for building or running EnergyPlus, on any platform. We will need CMake logic in place to have fallbacks for building EnergyPlus where it doesn't attempt to find a rendering library. And in EnergyPlus itself, we will need to have a switch for enabling specialized GPU-dependent calculations. I would love if a user who doesn't have OpenGL could still take advantage of pixel counting, if not for performance, then for proper shading evaluation of complicated zone geometry. With this in mind, I would like the input field for enabling this to specifically reflect "turning this on requires a graphics library on this machine", and then if E+ tries to run and doesn't find it, it gracefully fatals out and gives the user enough info to continue (either disable that feature or install the lib and re-run).

For the CI machines, as discussed yesterday, I am going to continue to push back against installing any additional libraries until we understand the consequences and packaging issues. We have in the past installed stuff on the machines without thinking about about these consequences, and ended up having a difficult time getting packages back to normal (Visual Studio drama of very late March 2017). Once we have all these issues answered, we can decide how to proceed. Eventually it seems important that we have a build on a clean machine that doesn't have any optional build dependencies on it, and then one that has all the optional build dependencies on it, but that would be a significant increase in burden, so I'm not sure we can completely support that. We'll figure it out.

Just know that I am in support of this new feature, I am just going to be playing slow and tight with the dependencies and the builds.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me.

Building: It will likely require some preprocessor directives to disable portions of code if the build machine doesn't have the linking graphics libraries. We'll likely also want to put something in the --version info to indicate whether a build does not support graphics processing. I can also have CMake issue a nice loud warning if OpenGL isn't found.

Running: This is already handled, but the messaging could use clean up. We'll need different messages depending on why it fails:

  1. the local machine doesn't have the required libraries, or
  2. the build wasn't linked to a graphics library.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @nealkruis, I should also point out that I don't expect any user to ever encounter a build of EnergyPlus that they find to not be built with support for the feature. The builds we produce as packages will have the capability built in so that we don't have stray packages lying around and confusion about whether a package has it or not.

The build issue is purely for developers. I don't want developers to have to install additional dependencies just to get a build going. This is the same thing as how our build allows BUILD_DOCS and BUILD_FORTRAN so that if a developer doesn't need to build those features, they don't have to install the stuff to do it. I imagine this is something that will be like BUILD_FOR_OPENGL or whatever, defaults to OFF, but can be flipped on easily. And our CI build scripts will set -DBUILD_FOR_OPENGL=ON. There's probably a niche case where an actual user wants to make a custom build that doesn't want this dependency also, but that would be the same as the developer use-case really.

No, I don't think BUILD_FOR_OPENGL is the right nomenclature 😆

For user alert, how about:

$ ./energyplus --config
{
  "program_name": "EnergyPlus",
  "version_number": "9.2.0",
  "build_sha": "abc123",
  "architecture": "64bit",
  "compiled_with": "Visual Studio 2019",
  "compile_date": "UTC 8601 Date",
  "build_options": [
    "open_gl_dependency": "true/false",
  ]
}

How cool would that be!!??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might lean toward making builds with OpenGL by default (since most developer machines will have the necessary libraries). It's really only Linux machines where there will be a problem.

What if we have BUILD_OPENGL_LINKS (working nomenclature) ON by default. If it's ON and CMake can't find OpenGL, CMake issues an error with a suggestion that the user either install a working implementation or turn BUILD_OPENGL_LINKS to OFF.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I could be convinced to default it to ON. In that case, I don't want it to be an advanced cmake variable so that it is easily accessible for disabling if someone should need to do it.

BUILD_OPENGL_LINKS seems like an improvement. We can keep noodling on that, but it seems to represent what we're doing.

@mjwitte
Copy link
Contributor

mjwitte commented Feb 20, 2020

@nealkruis @mbadams5 This needs a transition rule, and it's got a conflict now with SolarShading.cc

@mjwitte
Copy link
Contributor

mjwitte commented Feb 20, 2020

And CI results are probably meaningless at this point, given how far develop has moved in the last 24 hrs

@jasondegraw
Copy link
Member

@mjwitte Mark is working on the conflicts, some unfortunate choices were made in a recent PR

@@ -676,7 +676,7 @@ namespace SolarShading {
#else
auto error_callback = [](const int messageType, const std::string & message, void * /*contextPtr*/){
if (messageType == Pumbra::MSG_ERR) {
ShowFatalError(message);
ShowSevereError(message);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbadams5 I'm worried that this won't effectively handle problems within Penumbra.

@mjwitte
Copy link
Contributor

mjwitte commented Feb 21, 2020

This has a build warning.
It still needs text description of the transition in \src\Transition\InputRulesFiles\Rules9-2-0-to-9-3-0.md
And a description of the eio output changes for <Shadowing/Sun Position Calculations Annual Simulations> in \src\Transition\OutputRulesFiles\OutputChanges9-2-0-to-9-3-0.md

@mjwitte mjwitte added the Performance Includes code changes that are directed at improving the runtime performance of EnergyPlus label Feb 21, 2020
Copy link
Contributor

@mjwitte mjwitte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Tested transition with RefBldgFullServiceRestaurantNew2004_ChicagoTable, SolarShadingTest_DisableSelfShadingGroupTable, SolarShadingTest_DisableSelfShadingTable, SolarShadingTest_ImportedShadingTable. All transitioned properly and give same results as v9.2 (amazingly).

  2. This will need documentation (post-haste after I/O freeze).

  3. Re-built with OPENGL_REQUIRED, built fine with Win10 VS2019 16.4.5.

  4. Ran SolarShadingTest with PixelCounting option. It ran slower than PolygonClipping. Opened TaskManager and saw that it's using the onboard GPU, not the Nvidia card.
    image

Is there a system setting somewhere to get this to use the other GPU?

@nealkruis
Copy link
Member Author

@mjwitte

  1. Don't be too surprised 😉.
  2. Working on documentation now. Almost complete.
  3. Great!
  4. @mbadams5 might have more insight on this.

@mjwitte
Copy link
Contributor

mjwitte commented Feb 21, 2020

  1. Found the setting, had to manually switch the setting for EnergyPlus.exe
    image
    image

But maybe I can change the system default somewhere.

Anyways, with that setting changed I see activity in the Nvidia GPU.

@mbadams5
Copy link
Contributor

@mjwitte There is a way to change it system wide (https://stackoverflow.com/a/30515166), but I would typically allow your laptop to automatically switch between the two for battery savings. In that link, there is a way in code to force nvidia optimus (https://stackoverflow.com/a/27881472) to prefer the more powerful GPU and same for AMD (https://stackoverflow.com/a/28179913). We could add those to EnergyPlus but that could happen during bug fix since i'd want more testing around that added code to make sure nothing breaks.

@mjwitte
Copy link
Contributor

mjwitte commented Feb 21, 2020

@mbadams5 Thanks. I see I can get to application-specific settings in the Nvidia control panel as well. What's puzzling is that auto-select didn't use the Nvidia card even though it's running with the power plugged in. Anyways, I probably wouldn't try to change anything to force this for the 9.3 release, perhaps just highlight these settings in the docs for the PixelCounting option.

@mjwitte
Copy link
Contributor

mjwitte commented Feb 21, 2020

For a small run, like SolarShadingTest, the PixelCounting method is slower that PolygonClipping, as expected. But for a larger run, like \PerformanceTests\10x_incr_100zones1win10overhangsFullExIn (repaired and transitioned to 9.3, and output:variables turned off).

PolygonClipping:    ************* EnergyPlus Completed Successfully-- Elapsed Time=00hr 03min 21.08sec
PixelCounting:      ************* EnergyPlus Completed Successfully-- Elapsed Time=00hr 02min  6.60sec

That's 1.6x - nice! It was interesting to watch the sawtooth loading on the cpu and gpu as it switched between shading calcs and the rest of the simulation. And this was with shadow calcs every 20 days, it would only get better with more frequent shadow calcs. Will run with timestep frequency and report later (expectng PolygonClipping to take a while for that).
image

@Myoldmopar
Copy link
Member

👍 👍 👍 👍 👍 👍

@mjwitte
Copy link
Contributor

mjwitte commented Feb 21, 2020

Results just in . . . 10x_incr_100zones1win10overhangsFullExIn with timestep shadow calculations . . .
PolygonClipping 30:21, PixelCounting 8:42, 3.6x, woohoo!

And results are quite similar. This file has no HVAC, but window heat gain values in the Annual Building Sensible Heat Gain Components report vary by less than 1%.

Copy link
Contributor

@mjwitte mjwitte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start on the docs. It would be good to expand on the I/O Ref to discuss when it's useful to apply PixelCounting and how to maximize hardware performance, but that can wait until after I/O Freeze.

A few additional items noted below (but they, too, should wait).


This field is used to control how the solar, shading, and daylighting models are calculated with respect to the time of calculations during the simulation. The default and fastest method is selected using the keyword Periodic. A more detailed and slower method can be selected using the keyword Timestep. The Timestep method must be used for modeling dynamic fenestration and shading surfaces.

\paragraph{Field: Shading Calculation Update Frequency}\label{field-calculation-frequency}

This numeric field will cause the shadowing calculations to be done periodically using the number in the field as the number of days in each period. This field is only used if the default method AverageOverDaysInFrequency is used in the previous field. Using this field will allow you to synchronize the shadowing calculations with changes in shading devices. Using the default of 20 days in each period is the average number of days between significant changes in solar position angles. For these shadowing calculations, an ``average'' (over the time period) of solar angles, position, equation of time are also used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AverageOverDaysInFrequency --> Periodic

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, above, can you brag that PixelCounting works for FullInterior in non-convex zones? Or is that uncertain yet?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the engineering reference is a bit slim - ok for now.

@@ -29,8 +29,10 @@
HeatBalanceAlgorithm,ConductionTransferFunction;

ShadowCalculation,
AverageOverDaysInFrequency, !- Calculation Method
20; !- Calculation Frequency
PolygonClipping, !- Shading Calculation Method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also - after release - we should revert these (or maybe even remove them altogether @Myoldmopar ?) They aren't current and these changes will fully break them. I guess we try to figure out when they were last working correclyt and re-transition them, but that's low priority.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This comment applies only to the files in performance_tests/Inactive. The active idfs are all good.

### EIO output for ShadowCalculations

The eio output related to `ShadowCalculations` has been changed to reflect the new input fields. See Rules9-2-0-to-9-3-0.md.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, add the PR reference (later).

@@ -48,6 +48,30 @@ a) change "WCS" or "WorldCoordinateSystem" or "Absolute" to "World"

b) change "Rel\*" or "Relative\*" or "Local" to "Relative"

# Object Change: ShadowCalculations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@mjwitte mjwitte merged commit 327add2 into develop Feb 21, 2020
@mjwitte mjwitte deleted the penumbra-integration branch February 21, 2020 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IDDChange Code changes impact the IDD file (cannot be merged after IO freeze) NewFeature Includes code to add a new feature to EnergyPlus Performance Includes code changes that are directed at improving the runtime performance of EnergyPlus
Projects
None yet
Development

Successfully merging this pull request may close these issues.