We perform a systematic study on the decorrelation of anisotropic flows along the pseudorapidity in relativistic heavy-ion collisions at the LHC and RHIC energies. The dynamical evolution of the QGP fireball is simulated via the CLVisc (ideal) (3+1)-dimensional hydrodynamics model, with the fully fluctuating initial condition from A-Multi-Phase-Transport (AMPT) model. Detailed analysis is performed on the longitudinal decorrelations of elliptic, triangular and quadrangular flows in terms of flow vectors, flow magnitudes and flow orientations (event planes). It is found that pure flow magnitudes have smaller longitudinal decorrelation than pure flow orientations, and the decorrelation of flow vectors is a combined effect of both flow magnitudes and orientations. The longitudinal decorrelation of elliptic flow has a strong and non-monotonic centrality dependence due to the initial elliptic collision geometry: smallest decorrelation in mid-central collisions. In contrast, the decorrelations of triangular and quadrangular flows have weak centrality dependence, slightly larger decorrelations in more peripheral collisions. Our numerical results for Pb+Pb collisions at the LHC are in good agreement with the ATLAS data, while our RHIC results predict much larger longitudinal decorrelations as compared to the LHC. We further analyze the longitudinal structures of the AMPT initial conditions and find that the final-state longitudinal decorrelation effects are strongly correlated with the lengths of the initial string structures in the AMPT model. The decorrelation effects are typically larger at lower collision energies and in more peripheral collisions due to shorter lengths of the string structures in the initial states.