Reference documentation for deal.II version 9.5.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
step-48.h
Go to the documentation of this file.
1) const override
552 *   {
553 *   double t = this->get_time();
554 *  
555 *   const double m = 0.5;
556 *   const double c1 = 0.;
557 *   const double c2 = 0.;
558 *   const double factor =
559 *   (m / std::sqrt(1. - m * m) * std::sin(std::sqrt(1. - m * m) * t + c2));
560 *   double result = 1.;
561 *   for (unsigned int d = 0; d < dim; ++d)
562 *   result *= -4. * std::atan(factor / std::cosh(m * p[d] + c1));
563 *   return result;
564 *   }
565 *   };
566 *  
567 *  
568 *  
569 * @endcode
570 *
571 *
572 * <a name="SineGordonProblemclass"></a>
573 * <h3>SineGordonProblem class</h3>
574 *
575
576 *
577 * This is the main class that builds on the class in @ref step_25 "step-25". However, we
578 * replaced the SparseMatrix<double> class by the MatrixFree class to store
579 * the geometry data. Also, we use a distributed triangulation in this
580 * example.
581 *
582 * @code
583 *   template <int dim>
584 *   class SineGordonProblem
585 *   {
586 *   public:
587 *   SineGordonProblem();
588 *   void run();
589 *  
590 *   private:
591 *   ConditionalOStream pcout;
592 *  
593 *   void make_grid_and_dofs();
594 *   void output_results(const unsigned int timestep_number);
595 *  
596 *   #ifdef DEAL_II_WITH_P4EST
598 *   #else
600 *   #endif
601 *   FE_Q<dim> fe;
602 *   DoFHandler<dim> dof_handler;
603 *  
604 *   MappingQ1<dim> mapping;
605 *  
606 *   AffineConstraints<double> constraints;
607 *   IndexSet locally_relevant_dofs;
608 *  
609 *   MatrixFree<dim, double> matrix_free_data;
610 *  
611 *   LinearAlgebra::distributed::Vector<double> solution, old_solution,
612 *   old_old_solution;
613 *  
614 *   const unsigned int n_global_refinements;
615 *   double time, time_step;
616 *   const double final_time;
617 *   const double cfl_number;
618 *   const unsigned int output_timestep_skip;
619 *   };
620 *  
621 *  
622 * @endcode
623 *
624 *
625 * <a name="SineGordonProblemSineGordonProblem"></a>
626 * <h4>SineGordonProblem::SineGordonProblem</h4>
627 *
628
629 *
630 * This is the constructor of the SineGordonProblem class. The time interval
631 * and time step size are defined here. Moreover, we use the degree of the
632 * finite element that we defined at the top of the program to initialize a
633 * FE_Q finite element based on Gauss-Lobatto support points. These points
634 * are convenient because in conjunction with a QGaussLobatto quadrature
635 * rule of the same order they give a diagonal mass matrix without
636 * compromising accuracy too much (note that the integration is inexact,
637 * though), see also the discussion in the introduction. Note that FE_Q
638 * selects the Gauss-Lobatto nodal points by default due to their improved
639 * conditioning versus equidistant points. To make things more explicit, we
640 * state the selection of the nodal points nonetheless.
641 *
642 * @code
643 *   template <int dim>
644 *   SineGordonProblem<dim>::SineGordonProblem()
645 *   : pcout(std::cout, Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0)
646 *   #ifdef DEAL_II_WITH_P4EST
647 *   , triangulation(MPI_COMM_WORLD)
648 *   #endif
649 *   , fe(QGaussLobatto<1>(fe_degree + 1))
650 *   , dof_handler(triangulation)
651 *   , n_global_refinements(10 - 2 * dim)
652 *   , time(-10)
653 *   , time_step(10.)
654 *   , final_time(10.)
655 *   , cfl_number(.1 / fe_degree)
656 *   , output_timestep_skip(200)
657 *   {}
658 *  
659 * @endcode
660 *
661 *
662 * <a name="SineGordonProblemmake_grid_and_dofs"></a>
663 * <h4>SineGordonProblem::make_grid_and_dofs</h4>
664 *
665
666 *
667 * As in @ref step_25 "step-25" this functions sets up a cube grid in <code>dim</code>
668 * dimensions of extent @f$[-15,15]@f$. We refine the mesh more in the center of
669 * the domain since the solution is concentrated there. We first refine all
670 * cells whose center is within a radius of 11, and then refine once more
671 * for a radius 6. This simple ad hoc refinement could be done better by
672 * adapting the mesh to the solution using error estimators during the time
673 * stepping as done in other example programs, and using
674 * parallel::distributed::SolutionTransfer to transfer the solution to the
675 * new mesh.
676 *
677 * @code
678 *   template <int dim>
679 *   void SineGordonProblem<dim>::make_grid_and_dofs()
680 *   {
682 *   triangulation.refine_global(n_global_refinements);
683 *   {
685 *   cell = triangulation.begin_active(),
686 *   end_cell = triangulation.end();
687 *   for (; cell != end_cell; ++cell)
688 *   if (cell->is_locally_owned())
689 *   if (cell->center().norm() < 11)
690 *   cell->set_refine_flag();
691 *   triangulation.execute_coarsening_and_refinement();
692 *  
693 *   cell = triangulation.begin_active();
694 *   end_cell = triangulation.end();
695 *   for (; cell != end_cell; ++cell)
696 *   if (cell->is_locally_owned())
697 *   if (cell->center().norm() < 6)
698 *   cell->set_refine_flag();
699 *   triangulation.execute_coarsening_and_refinement();
700 *   }
701 *  
702 *   pcout << " Number of global active cells: "
703 *   #ifdef DEAL_II_WITH_P4EST
704 *   << triangulation.n_global_active_cells()
705 *   #else
706 *   << triangulation.n_active_cells()
707 *   #endif
708 *   << std::endl;
709 *  
710 *   dof_handler.distribute_dofs(fe);
711 *  
712 *   pcout << " Number of degrees of freedom: " << dof_handler.n_dofs()
713 *   << std::endl;
714 *  
715 *  
716 * @endcode
717 *
718 * We generate hanging node constraints for ensuring continuity of the
719 * solution. As in @ref step_40 "step-40", we need to equip the constraint matrix with
720 * the IndexSet of locally relevant degrees of freedom to avoid it to
721 * consume too much memory for big problems. Next, the <code> MatrixFree
722 * </code> object for the problem is set up. Note that we specify a
723 * particular scheme for shared-memory parallelization (hence one would
724 * use multithreading for intra-node parallelism and not MPI; we here
725 * choose the standard option &mdash; if we wanted to disable shared
726 * memory parallelization even in case where there is more than one TBB
727 * thread available in the program, we would choose
729 * instead of using the default QGauss quadrature argument, we supply a
730 * QGaussLobatto quadrature formula to enable the desired
731 * behavior. Finally, three solution vectors are initialized. MatrixFree
732 * expects a particular layout of ghost indices (as it handles index
733 * access in MPI-local numbers that need to match between the vector and
734 * MatrixFree), so we just ask it to initialize the vectors to be sure the
735 * ghost exchange is properly handled.
736 *
737 * @code
738 *   locally_relevant_dofs =
740 *   constraints.clear();
741 *   constraints.reinit(locally_relevant_dofs);
742 *   DoFTools::make_hanging_node_constraints(dof_handler, constraints);
743 *   constraints.close();
744 *  
745 *   typename MatrixFree<dim>::AdditionalData additional_data;
746 *   additional_data.tasks_parallel_scheme =
748 *  
749 *   matrix_free_data.reinit(mapping,
750 *   dof_handler,
751 *   constraints,
752 *   QGaussLobatto<1>(fe_degree + 1),
753 *   additional_data);
754 *  
755 *   matrix_free_data.initialize_dof_vector(solution);
756 *   old_solution.reinit(solution);
757 *   old_old_solution.reinit(solution);
758 *   }
759 *  
760 *  
761 *  
762 * @endcode
763 *
764 *
765 * <a name="SineGordonProblemoutput_results"></a>
766 * <h4>SineGordonProblem::output_results</h4>
767 *
768
769 *
770 * This function prints the norm of the solution and writes the solution
771 * vector to a file. The norm is standard (except for the fact that we need
772 * to accumulate the norms over all processors for the parallel grid which
773 * we do via the VectorTools::compute_global_error() function), and the
774 * second is similar to what we did in @ref step_40 "step-40" or @ref step_37 "step-37". Note that we can
775 * use the same vector for output as the one used during computations: The
776 * vectors in the matrix-free framework always provide full information on
777 * all locally owned cells (this is what is needed in the local evaluations,
778 * too), including ghost vector entries on these cells. This is the only
779 * data that is needed in the VectorTools::integrate_difference() function
780 * as well as in DataOut. The only action to take at this point is to make
781 * sure that the vector updates its ghost values before we read from
782 * them, and to reset ghost values once done. This is a feature present only
783 * in the LinearAlgebra::distributed::Vector class. Distributed vectors with
784 * PETSc and Trilinos, on the other hand, need to be copied to special
785 * vectors including ghost values (see the relevant section in @ref step_40 "step-40"). If
786 * we also wanted to access all degrees of freedom on ghost cells (e.g. when
787 * computing error estimators that use the jump of solution over cell
788 * boundaries), we would need more information and create a vector
789 * initialized with locally relevant dofs just as in @ref step_40 "step-40". Observe also
790 * that we need to distribute constraints for output - they are not filled
791 * during computations (rather, they are interpolated on the fly in the
792 * matrix-free method FEEvaluation::read_dof_values()).
793 *
794 * @code
795 *   template <int dim>
796 *   void
797 *   SineGordonProblem<dim>::output_results(const unsigned int timestep_number)
798 *   {
799 *   constraints.distribute(solution);
800 *  
801 *   Vector<float> norm_per_cell(triangulation.n_active_cells());
802 *   solution.update_ghost_values();
804 *   dof_handler,
805 *   solution,
807 *   norm_per_cell,
808 *   QGauss<dim>(fe_degree + 1),
810 *   const double solution_norm =
812 *   norm_per_cell,
814 *  
815 *   pcout << " Time:" << std::setw(8) << std::setprecision(3) << time
816 *   << ", solution norm: " << std::setprecision(5) << std::setw(7)
817 *   << solution_norm << std::endl;
818 *  
819 *   DataOut<dim> data_out;
820 *  
821 *   data_out.attach_dof_handler(dof_handler);
822 *   data_out.add_data_vector(solution, "solution");
823 *   data_out.build_patches(mapping);
824 *  
825 *   data_out.write_vtu_with_pvtu_record(
826 *   "./", "solution", timestep_number, MPI_COMM_WORLD, 3);
827 *  
828 *   solution.zero_out_ghost_values();
829 *   }
830 *  
831 *  
832 * @endcode
833 *
834 *
835 * <a name="SineGordonProblemrun"></a>
836 * <h4>SineGordonProblem::run</h4>
837 *
838
839 *
840 * This function is called by the main function and steps into the
841 * subroutines of the class.
842 *
843
844 *
845 * After printing some information about the parallel setup, the first
846 * action is to set up the grid and the cell operator. Then, the time step
847 * is computed from the CFL number given in the constructor and the finest
848 * mesh size. The finest mesh size is computed as the diameter of the last
849 * cell in the triangulation, which is the last cell on the finest level of
850 * the mesh. This is only possible for meshes where all elements on a level
851 * have the same size, otherwise, one needs to loop over all cells. Note
852 * that we need to query all the processors for their finest cell since
853 * not all processors might hold a region where the mesh is at the finest
854 * level. Then, we readjust the time step a little to hit the final time
855 * exactly.
856 *
857 * @code
858 *   template <int dim>
859 *   void SineGordonProblem<dim>::run()
860 *   {
861 *   {
862 *   pcout << "Number of MPI ranks: "
863 *   << Utilities::MPI::n_mpi_processes(MPI_COMM_WORLD) << std::endl;
864 *   pcout << "Number of threads on each rank: "
865 *   << MultithreadInfo::n_threads() << std::endl;
866 *   const unsigned int n_vect_doubles = VectorizedArray<double>::size();
867 *   const unsigned int n_vect_bits = 8 * sizeof(double) * n_vect_doubles;
868 *   pcout << "Vectorization over " << n_vect_doubles
869 *   << " doubles = " << n_vect_bits << " bits ("
871 *   << std::endl
872 *   << std::endl;
873 *   }
874 *   make_grid_and_dofs();
875 *  
876 *   const double local_min_cell_diameter =
877 *   triangulation.last()->diameter() / std::sqrt(dim);
878 *   const double global_min_cell_diameter =
879 *   -Utilities::MPI::max(-local_min_cell_diameter, MPI_COMM_WORLD);
880 *   time_step = cfl_number * global_min_cell_diameter;
881 *   time_step = (final_time - time) / (int((final_time - time) / time_step));
882 *   pcout << " Time step size: " << time_step
883 *   << ", finest cell: " << global_min_cell_diameter << std::endl
884 *   << std::endl;
885 *  
886 * @endcode
887 *
888 * Next the initial value is set. Since we have a two-step time stepping
889 * method, we also need a value of the solution at time-time_step. For
890 * accurate results, one would need to compute this from the time
891 * derivative of the solution at initial time, but here we ignore this
892 * difficulty and just set it to the initial value function at that
893 * artificial time.
894 *
895
896 *
897 * We then go on by writing the initial state to file and collecting
898 * the two starting solutions in a <tt>std::vector</tt> of pointers that
899 * get later consumed by the SineGordonOperation::apply() function. Next,
900 * an instance of the <code> SineGordonOperation class </code> based on
901 * the finite element degree specified at the top of this file is set up.
902 *
903 * @code
904 *   VectorTools::interpolate(mapping,
905 *   dof_handler,
906 *   InitialCondition<dim>(1, time),
907 *   solution);
908 *   VectorTools::interpolate(mapping,
909 *   dof_handler,
910 *   InitialCondition<dim>(1, time - time_step),
911 *   old_solution);
912 *   output_results(0);
913 *  
914 *   std::vector<LinearAlgebra::distributed::Vector<double> *>
915 *   previous_solutions({&old_solution, &old_old_solution});
916 *  
917 *   SineGordonOperation<dim, fe_degree> sine_gordon_op(matrix_free_data,
918 *   time_step);
919 *  
920 * @endcode
921 *
922 * Now loop over the time steps. In each iteration, we shift the solution
923 * vectors by one and call the `apply` function of the
924 * `SineGordonOperator` class. Then, we write the solution to a file. We
925 * clock the wall times for the computational time needed as wall as the
926 * time needed to create the output and report the numbers when the time
927 * stepping is finished.
928 *
929
930 *
931 * Note how this shift is implemented: We simply call the swap method on
932 * the two vectors which swaps only some pointers without the need to copy
933 * data around, a relatively expensive operation within an explicit time
934 * stepping method. Let us see what happens in more detail: First, we
935 * exchange <code>old_solution</code> with <code>old_old_solution</code>,
936 * which means that <code>old_old_solution</code> gets
937 * <code>old_solution</code>, which is what we expect. Similarly,
938 * <code>old_solution</code> gets the content from <code>solution</code>
939 * in the next step. After this, <code>solution</code> holds
940 * <code>old_old_solution</code>, but that will be overwritten during this
941 * step.
942 *
943 * @code
944 *   unsigned int timestep_number = 1;
945 *  
946 *   Timer timer;
947 *   double wtime = 0;
948 *   double output_time = 0;
949 *   for (time += time_step; time <= final_time;
950 *   time += time_step, ++timestep_number)
951 *   {
952 *   timer.restart();
953 *   old_old_solution.swap(old_solution);
954 *   old_solution.swap(solution);
955 *   sine_gordon_op.apply(solution, previous_solutions);
956 *   wtime += timer.wall_time();
957 *  
958 *   timer.restart();
959 *   if (timestep_number % output_timestep_skip == 0)
960 *   output_results(timestep_number / output_timestep_skip);
961 *  
962 *   output_time += timer.wall_time();
963 *   }
964 *   timer.restart();
965 *   output_results(timestep_number / output_timestep_skip + 1);
966 *   output_time += timer.wall_time();
967 *  
968 *   pcout << std::endl
969 *   << " Performed " << timestep_number << " time steps." << std::endl;
970 *  
971 *   pcout << " Average wallclock time per time step: "
972 *   << wtime / timestep_number << 's' << std::endl;
973 *  
974 *   pcout << " Spent " << output_time << "s on output and " << wtime
975 *   << "s on computations." << std::endl;
976 *   }
977 *   } // namespace Step48
978 *  
979 *  
980 *  
981 * @endcode
982 *
983 *
984 * <a name="Thecodemaincodefunction"></a>
985 * <h3>The <code>main</code> function</h3>
986 *
987
988 *
989 * As in @ref step_40 "step-40", we initialize MPI at the start of the program. Since we will
990 * in general mix MPI parallelization with threads, we also set the third
991 * argument in MPI_InitFinalize that controls the number of threads to an
992 * invalid number, which means that the TBB library chooses the number of
993 * threads automatically, typically to the number of available cores in the
994 * system. As an alternative, you can also set this number manually if you
995 * want to set a specific number of threads (e.g. when MPI-only is required).
996 *
997 * @code
998 *   int main(int argc, char **argv)
999 *   {
1000 *   using namespace Step48;
1001 *   using namespace dealii;
1002 *  
1003 *   Utilities::MPI::MPI_InitFinalize mpi_initialization(
1004 *   argc, argv, numbers::invalid_unsigned_int);
1005 *  
1006 *   try
1007 *   {
1008 *   SineGordonProblem<dimension> sg_problem;
1009 *   sg_problem.run();
1010 *   }
1011 *   catch (std::exception &exc)
1012 *   {
1013 *   std::cerr << std::endl
1014 *   << std::endl
1015 *   << "----------------------------------------------------"
1016 *   << std::endl;
1017 *   std::cerr << "Exception on processing: " << std::endl
1018 *   << exc.what() << std::endl
1019 *   << "Aborting!" << std::endl
1020 *   << "----------------------------------------------------"
1021 *   << std::endl;
1022 *  
1023 *   return 1;
1024 *   }
1025 *   catch (...)
1026 *   {
1027 *   std::cerr << std::endl
1028 *   << std::endl
1029 *   << "----------------------------------------------------"
1030 *   << std::endl;
1031 *   std::cerr << "Unknown exception!" << std::endl
1032 *   << "Aborting!" << std::endl
1033 *   << "----------------------------------------------------"
1034 *   << std::endl;
1035 *   return 1;
1036 *   }
1037 *  
1038 *   return 0;
1039 *   }
1040 * @endcode
1041<a name="Results"></a><h1>Results</h1>
1042
1043
1044<a name="Comparisonwithasparsematrix"></a><h3>Comparison with a sparse matrix</h3>
1045
1046
1047In order to demonstrate the gain in using the MatrixFree class instead of
1048the standard <code>deal.II</code> assembly routines for evaluating the
1049information from old time steps, we study a simple serial run of the code on a
1050nonadaptive mesh. Since much time is spent on evaluating the sine function, we
1051do not only show the numbers of the full sine-Gordon equation but also for the
1052wave equation (the sine-term skipped from the sine-Gordon equation). We use
1053both second and fourth order elements. The results are summarized in the
1054following table.
1055
1056<table align="center" class="doxtable">
1057 <tr>
1058 <th>&nbsp;</th>
1059 <th colspan="3">wave equation</th>
1060 <th colspan="2">sine-Gordon</th>
1061 </tr>
1062 <tr>
1063 <th>&nbsp;</th>
1064 <th>MF</th>
1065 <th>SpMV</th>
1066 <th>dealii</th>
1067 <th>MF</th>
1068 <th>dealii</th>
1069 </tr>
1070 <tr>
1071 <td>2D, @f$\mathcal{Q}_2@f$</td>
1072 <td align="right"> 0.0106</td>
1073 <td align="right"> 0.00971</td>
1074 <td align="right"> 0.109</td>
1075 <td align="right"> 0.0243</td>
1076 <td align="right"> 0.124</td>
1077 </tr>
1078 <tr>
1079 <td>2D, @f$\mathcal{Q}_4@f$</td>
1080 <td align="right"> 0.0328</td>
1081 <td align="right"> 0.0706</td>
1082 <td align="right"> 0.528</td>
1083 <td align="right"> 0.0714</td>
1084 <td align="right"> 0.502</td>
1085 </tr>
1086 <tr>
1087 <td>3D, @f$\mathcal{Q}_2@f$</td>
1088 <td align="right"> 0.0151</td>
1089 <td align="right"> 0.0320</td>
1090 <td align="right"> 0.331</td>
1091 <td align="right"> 0.0376</td>
1092 <td align="right"> 0.364</td>
1093 </tr>
1094 <tr>
1095 <td>3D, @f$\mathcal{Q}_4@f$</td>
1096 <td align="right"> 0.0918</td>
1097 <td align="right"> 0.844</td>
1098 <td align="right"> 6.83</td>
1099 <td align="right"> 0.194</td>
1100 <td align="right"> 6.95</td>
1101 </tr>
1102</table>
1103
1104It is apparent that the matrix-free code outperforms the standard assembly
1105routines in deal.II by far. In 3D and for fourth order elements, one operator
1106evaluation is also almost ten times as fast as a sparse matrix-vector
1107product.
1108
1109<a name="Parallelrunin2Dand3D"></a><h3>Parallel run in 2D and 3D</h3>
1110
1111
1112We start with the program output obtained on a workstation with 12 cores / 24
1113threads (one Intel Xeon E5-2687W v4 CPU running at 3.2 GHz, hyperthreading
1114enabled), running the program in release mode:
1115@code
1116\$ make run
1117Number of MPI ranks: 1
1118Number of threads on each rank: 24
1119Vectorization over 4 doubles = 256 bits (AVX)
1120
1121 Number of global active cells: 15412
1122 Number of degrees of freedom: 249065
1123 Time step size: 0.00292997, finest cell: 0.117188
1124
1125 Time: -10, solution norm: 9.5599
1126 Time: -9.41, solution norm: 17.678
1127 Time: -8.83, solution norm: 23.504
1128 Time: -8.24, solution norm: 27.5
1129 Time: -7.66, solution norm: 29.513
1130 Time: -7.07, solution norm: 29.364
1131 Time: -6.48, solution norm: 27.23
1132 Time: -5.9, solution norm: 23.527
1133 Time: -5.31, solution norm: 18.439
1134 Time: -4.73, solution norm: 11.935
1135 Time: -4.14, solution norm: 5.5284
1136 Time: -3.55, solution norm: 8.0354
1137 Time: -2.97, solution norm: 14.707
1138 Time: -2.38, solution norm: 20
1139 Time: -1.8, solution norm: 22.834
1140 Time: -1.21, solution norm: 22.771
1141 Time: -0.624, solution norm: 20.488
1142 Time: -0.0381, solution norm: 16.697
1143 Time: 0.548, solution norm: 11.221
1144 Time: 1.13, solution norm: 5.3912
1145 Time: 1.72, solution norm: 8.4528
1146 Time: 2.31, solution norm: 14.335
1147 Time: 2.89, solution norm: 18.555
1148 Time: 3.48, solution norm: 20.894
1149 Time: 4.06, solution norm: 21.305
1150 Time: 4.65, solution norm: 19.903
1151 Time: 5.24, solution norm: 16.864
1152 Time: 5.82, solution norm: 12.223
1153 Time: 6.41, solution norm: 6.758
1154 Time: 6.99, solution norm: 7.2423
1155 Time: 7.58, solution norm: 12.888
1156 Time: 8.17, solution norm: 17.273
1157 Time: 8.75, solution norm: 19.654
1158 Time: 9.34, solution norm: 19.838
1159 Time: 9.92, solution norm: 17.964
1160 Time: 10, solution norm: 17.595
1161
1162 Performed 6826 time steps.
1163 Average wallclock time per time step: 0.0013453s
1164 Spent 14.976s on output and 9.1831s on computations.
1165@endcode
1166
1167In 3D, the respective output looks like
1168@code
1169\$ make run
1170Number of MPI ranks: 1
1171Number of threads on each rank: 24
1172Vectorization over 4 doubles = 256 bits (AVX)
1173
1174 Number of global active cells: 17592
1175 Number of degrees of freedom: 1193881
1176 Time step size: 0.0117233, finest cell: 0.46875
1177
1178 Time: -10, solution norm: 29.558
1179 Time: -7.66, solution norm: 129.13
1180 Time: -5.31, solution norm: 67.753
1181 Time: -2.97, solution norm: 79.245
1182 Time: -0.621, solution norm: 123.52
1183 Time: 1.72, solution norm: 43.525
1184 Time: 4.07, solution norm: 93.285
1185 Time: 6.41, solution norm: 97.722
1186 Time: 8.76, solution norm: 36.734
1187 Time: 10, solution norm: 94.115
1188
1189 Performed 1706 time steps.
1190 Average wallclock time per time step: 0.0084542s
1191 Spent 16.766s on output and 14.423s on computations.
1192@endcode
1193
1194It takes 0.008 seconds for one time step with more than a million
1195degrees of freedom (note that we would need many processors to reach such
1196numbers when solving linear systems).
1197
1198If we replace the thread-parallelization by a pure MPI parallelization, the
1199timings change into:
1200@code
1201\$ mpirun -n 24 ./step-48
1202Number of MPI ranks: 24
1203Number of threads on each rank: 1
1204Vectorization over 4 doubles = 256 bits (AVX)
1205...
1206 Performed 1706 time steps.
1207 Average wallclock time per time step: 0.0051747s
1208 Spent 2.0535s on output and 8.828s on computations.
1209@endcode
1210
1211We observe a dramatic speedup for the output (which makes sense, given that
1212most code of the output is not parallelized via threads, whereas it is for
1213MPI), but less than the theoretical factor of 12 we would expect from the
1214parallelism. More interestingly, the computations also get faster when
1215switching from the threads-only variant to the MPI-only variant. This is a
1216general observation for the MatrixFree framework (as of updating this data in
12172019). The main reason is that the decisions regarding work on conflicting
1218cell batches made to enable execution in parallel are overly pessimistic:
1219While they ensure that no work on neighboring cells is done on different
1220threads at the same time, this conservative setting implies that data from
1221neighboring cells is also evicted from caches by the time neighbors get
1222touched. Furthermore, the current scheme is not able to provide a constant
1223load for all 24 threads for the given mesh with 17,592 cells.
1224
1225The current program allows to also mix MPI parallelization with thread
1226parallelization. This is most beneficial when running programs on clusters
1227with multiple nodes, using MPI for the inter-node parallelization and threads
1228for the intra-node parallelization. On the workstation used above, we can run
1229threads in the hyperthreading region (i.e., using 2 threads for each of the 12
1230MPI ranks). An important setting for mixing MPI with threads is to ensure
1231proper binning of tasks to CPUs. On many clusters the placing is either
1232automatically via the `mpirun/mpiexec` environment, or there can be manual
1233settings. Here, we simply report the run times the plain version of the
1234program (noting that things could be improved towards the timings of the
1235MPI-only program when proper pinning is done):
1236@code
1237\$ mpirun -n 12 ./step-48
1238Number of MPI ranks: 12
1239Number of threads on each rank: 2
1240Vectorization over 4 doubles = 256 bits (AVX)
1241...
1242 Performed 1706 time steps.
1243 Average wallclock time per time step: 0.0056651s
1244 Spent 2.5175s on output and 9.6646s on computations.
1245@endcode
1246
1247
1248
1249<a name="Possibilitiesforextensions"></a><h3>Possibilities for extensions</h3>
1250
1251
1252There are several things in this program that could be improved to make it
1253even more efficient (besides improved boundary conditions and physical
1254stuff as discussed in @ref step_25 "step-25"):
1255
1256<ul> <li> <b>Faster evaluation of sine terms:</b> As becomes obvious
1257 from the comparison of the plain wave equation and the sine-Gordon
1258 equation above, the evaluation of the sine terms dominates the total
1259 time for the finite element operator application. There are a few
1260 reasons for this: Firstly, the deal.II sine computation of a
1261 VectorizedArray field is not vectorized (as opposed to the rest of
1262 the operator application). This could be cured by handing the sine
1263 computation to a library with vectorized sine computations like
1264 Intel's math kernel library (MKL). By using the function
1265 <code>vdSin</code> in MKL, the program uses half the computing time
1266 in 2D and 40 percent less time in 3D. On the other hand, the sine
1267 computation is structurally much more complicated than the simple
1268 arithmetic operations like additions and multiplications in the rest
1269 of the local operation.
1270
1271 <li> <b>Higher order time stepping:</b> While the implementation allows for
1272 arbitrary order in the spatial part (by adjusting the degree of the finite
1273 element), the time stepping scheme is a standard second-order leap-frog
1274 scheme. Since solutions in wave propagation problems are usually very
1275 smooth, the error is likely dominated by the time stepping part. Of course,
1276 this could be cured by using smaller time steps (at a fixed spatial
1277 resolution), but it would be more efficient to use higher order time
1278 stepping as well. While it would be straight-forward to do so for a
1279 first-order system (use some Runge&ndash;Kutta scheme of higher order,
1280 probably combined with adaptive time step selection like the <a
1281 href="http://en.wikipedia.org/wiki/Dormand%E2%80%93Prince_method">Dormand&ndash;Prince
1282 method</a>), it is more challenging for the second-order formulation. At
1283 least in the finite difference community, people usually use the PDE to find
1284 spatial correction terms that improve the temporal error.
1285
1286</ul>
1287 *
1288 *
1289<a name="PlainProg"></a>
1290<h1> The plain program</h1>
1291@include "step-48.cc"
1292*/
void attach_dof_handler(const DoFHandler< dim, spacedim > &)
Definition fe_q.h:551
void reinit(const MappingType &mapping, const DoFHandler< dim > &dof_handler, const AffineConstraints< number2 > &constraint, const QuadratureType &quad, const AdditionalData &additional_data=AdditionalData())
static unsigned int n_threads()
Definition timer.h:118
void restart()
Definition timer.h:897
Point< 3 > center
Point< 2 > second
Definition grid_out.cc:4616
Point< 2 > first
Definition grid_out.cc:4615
unsigned int level
Definition grid_out.cc:4618
__global__ void set(Number *val, const Number s, const size_type N)
void loop(ITERATOR begin, std_cxx20::type_identity_t< ITERATOR > end, DOFINFO &dinfo, INFOBOX &info, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &cell_worker, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &boundary_worker, const std::function< void(DOFINFO &, DOFINFO &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, ASSEMBLER &assembler, const LoopControl &lctrl=LoopControl())
Definition loop.h:439
void make_hanging_node_constraints(const DoFHandler< dim, spacedim > &dof_handler, AffineConstraints< number > &constraints)
const Event initial
Definition event.cc:65
void apply(const Kokkos::TeamPolicy< MemorySpace::Default::kokkos_space::execution_space >::member_type &team_member, const Kokkos::View< Number *, MemorySpace::Default::kokkos_space > shape_data, const ViewTypeIn in, ViewTypeOut out)
IndexSet extract_locally_relevant_dofs(const DoFHandler< dim, spacedim > &dof_handler)
void hyper_cube(Triangulation< dim, spacedim > &tria, const double left=0., const double right=1., const bool colorize=false)
void refine(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double threshold, const unsigned int max_to_mark=numbers::invalid_unsigned_int)
void shift(const Tensor< 1, spacedim > &shift_vector, Triangulation< dim, spacedim > &triangulation)
double diameter(const Triangulation< dim, spacedim > &tria)
Definition grid_tools.cc:88
@ matrix
Contents is actually a matrix.
@ diagonal
Matrix is diagonal.
@ general
No special properties.
double norm(const FEValuesBase< dim > &fe, const ArrayView< const std::vector< Tensor< 1, dim > > > &Du)
Definition divergence.h:472
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
void call(const std::function< RT()> &function, internal::return_value< RT > &ret_val)
void free(T *&pointer)
Definition cuda.h:97
std::vector< unsigned int > serial(const std::vector< unsigned int > &targets, const std::function< RequestType(const unsigned int)> &create_request, const std::function< AnswerType(const unsigned int, const RequestType &)> &answer_request, const std::function< void(const unsigned int, const AnswerType &)> &process_answer, const MPI_Comm comm)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
Definition mpi.cc:150
T max(const T &t, const MPI_Comm mpi_communicator)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
Definition mpi.cc:161
std::string get_time()
const std::string get_current_vectorization_level()
Definition utilities.cc:939
double compute_global_error(const Triangulation< dim, spacedim > &tria, const InVector &cellwise_error, const NormType &norm, const double exponent=2.)
void integrate_difference(const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof, const InVector &fe_function, const Function< spacedim, typename InVector::value_type > &exact_solution, OutVector &difference, const Quadrature< dim > &q, const NormType &norm, const Function< spacedim, double > *weight=nullptr, const double exponent=2.)
void run(const Iterator &begin, const std_cxx20::type_identity_t< Iterator > &end, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length, const unsigned int chunk_size)
void copy(const T *begin, const T *end, U *dest)
int(&) functions(const void *v1, const void *v2)
static const unsigned int invalid_unsigned_int
Definition types.h:213
STL namespace.
::VectorizedArray< Number, width > sin(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation
void swap(SmartPointer< T, P > &t1, SmartPointer< T, Q > &t2)
TasksParallelScheme tasks_parallel_scheme
const TriangulationDescription::Settings settings